Java GetInputStream方法不返回

时间:2012-05-17 16:17:36

标签: java infinite-loop urlconnection

我有Java代码来测试代理是SOCKS类型还是HTTP类型,但我注意到在检查它是否为SOCKS时,代码在调用方法connection.getInputStream()后没有返回。这个问题的原因是什么,我该如何解决?

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);
}
}

0 个答案:

没有答案