1

プロキシのタイプがSOCKSかHTTPかをテストするJavaコードがありますが、プロキシが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);
}
}
4

0 に答える 0