开发者

Checking the type of a web proxy

开发者 https://www.devze.com 2023-03-21 18:11 出处:网络
How do 开发者_如何学CI determine whether a web proxy IP is of type HTTP or SOCKS4/5 with java? Thank you.As mentioned in the comments from my other answer, if you know the IP address of a proxy serve

How do 开发者_如何学CI determine whether a web proxy IP is of type HTTP or SOCKS4/5 with java?

Thank you.


As mentioned in the comments from my other answer, if you know the IP address of a proxy server and want to detect what type it is, you could try each proxy type in Java until one works.

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.List;

public class ProxyTest
{
    public static void main(String... args)
    throws IOException
    {
        InetSocketAddress proxyAddress = new InetSocketAddress("myproxyaddress", 1234);
        Proxy.Type proxyType = detectProxyType(proxyAddress);
        System.out.println(proxyAddress + " is a " + proxyType + " proxy.");
    }

    public static Proxy.Type detectProxyType(InetSocketAddress proxyAddress)
    throws IOException
    {
        URL url = new URL("http://www.google.com");
        List<Proxy.Type> proxyTypesToTry = Arrays.asList(Proxy.Type.SOCKS, Proxy.Type.HTTP);

        for (Proxy.Type proxyType : proxyTypesToTry)
        {
            Proxy proxy = new Proxy(proxyType, proxyAddress);

            //Try with SOCKS
            URLConnection connection = null;
            try
            {
                connection = url.openConnection(proxy);

                //Can modify timeouts if default timeout is taking too long
                //connection.setConnectTimeout(1000);
                //connection.setReadTimeout(1000);

                connection.getContent();

                //If we get here we made a successful connection
                return(proxyType);
            }
            catch (SocketException e) //or possibly more generic IOException?
            {
                //Proxy connection failed
            }
        }

        //No proxies worked if we get here
        return(null);
    }
}

In this code, it first tries to connect to www.google.com using the proxy at myproxyaddress with SOCKS, and if that fails it will try using it as an HTTP proxy, returning the method that worked, or null if none worked.


If you want to determine the type of proxy being used from Java, you can use ProxySelector and Proxy.

e.g.

import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.util.List;

public class ProxyTest
{
    public static void main(String... args)
    {
        System.setProperty("java.net.useSystemProxies", "true");

        List<Proxy> proxyList = ProxySelector.getDefault().select(URI.create("http://www.google.com"));
        if (!proxyList.isEmpty())
        {
            Proxy proxy = proxyList.get(0);
            switch (proxy.type())
            {
                case DIRECT:
                    System.out.println("Direct connection - no proxy.");
                    break;
                case HTTP:
                    System.out.println("HTTP proxy: " + proxy.address());
                    break;
                case SOCKS:
                    System.out.println("SOCKS proxy: " + proxy.address());
                    break;
            }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号