Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have Java code that tests whether a proxy is of type SOCKS or HTTP, but I noticed that when it checks if it's SOCKS the code does not return after calling the method connection.getInputStream(). What is the cause of this problem and how can I solve it?

import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.commons.codec.binary.Base64;

public class ProxyChecker {

private final int timeout = (10 * 1000);

public synchronized void check(String ip, int port, String username, String password) {

    try {

        InetSocketAddress proxyAddress = new InetSocketAddress(ip, port);

        URL url = new URL("http://www.my-test-url-here.com/");

        List<Proxy.Type> proxyTypes = Arrays.asList(Proxy.Type.SOCKS, Proxy.Type.HTTP);

        for(Proxy.Type proxyType : proxyTypes) {

            Proxy proxy = new Proxy(proxyType, proxyAddress);

            try {

                long time = System.currentTimeMillis();

                URLConnection connection = url.openConnection(proxy);

                connection.setReadTimeout(timeout);
                connection.setConnectTimeout(timeout);

                connection.setRequestProperty("User-Agent", "My User-Agent");

                if(username != null && password != null) {

                    String encoded = new String(Base64.encodeBase64(new String(username + ":" + password).getBytes()));

                    connection.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
                }

                String line = "";

                // connection.getInputStream() not returning
                System.out.println("Getting InputStream...");

                InputStream in = connection.getInputStream();

                System.out.println("Got InputStream.");

                StringBuffer lineBuffer = new StringBuffer();
                BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );

                while ((line = reader.readLine()) != null) {

                    lineBuffer.append(line);
                }

                String response = lineBuffer.toString();

                System.out.println("Page: " + response);

                reader.close();
                in.close();

                int elapse = (int)(System.currentTimeMillis() - time);

                //If we get here we made a successful connection

                if(proxyType == Proxy.Type.HTTP) {

                    System.out.println("Proxy is Type HTTP");

                } else if(proxyType == Proxy.Type.SOCKS) {

                    System.out.println("Proxy is Type SOCKS");
                }

            } catch (SocketException se) {

                se.printStackTrace();

            } catch (IOException ioe) {

                ioe.printStackTrace();
            }
        }

    } catch(Exception e) {

        e.printStackTrace();
    }
}

public static void main(String args[]) {

    ProxyChecker proxyChecker = new ProxyChecker();

    //proxyChecker.check("127.0.0.1", 8080, "admin", "admin");

    proxyChecker.check("127.0.0.1", 80, null, null);
}
}
share|improve this question
4  
If you set a read timeout of say, 10 seconds, is a SocketTimeoutException thrown? To set a read timeout of 10 seconds, call connection.setReadTimeout(10 * 1000). – Matt Hurne May 17 '12 at 16:27
I'm using the setReadTimeout in the code above, that did not solve the problem. – john May 17 '12 at 19:40
Was it there originally? If so, sorry I missed it! – Matt Hurne May 18 '12 at 12:00
add comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.