Tuesday, May 12, 2015

How To Use both HTTP and Socks Proxy In Java

The simplest way is use global setting, for example you can specify the proxy in command line.
java -Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080
-DsocksProxyHost=socks.example.com GetURL

You can also configure proxy in you java code.
System.setProperty("http.proxyHost", "webcache.example.com");
System.setProperty("http.proxyPort", "8080");

By the ways above, you can use one proxy only. If you want to connect proxy requires username and password, or select proxy for each connection, please refer to java sample code below.

Note

1.       If you use HTTP proxy, you can use many proxies in your single java process. For example, Thread A connect to Proxy P1, and Thread B connect to Proxy P2.
2.       If you use Socks proxy, the socks proxy setting is global setting. In other words, only one proxy is supported. So if you change proxy, the setting will take effect in all threads.

Sample java code that use multi-proxy with authentication.

The EasyHA supports  both Http and Socks proxy. Perhaps you want to upload data to cloud, and your server is behind proxy, so you need to connect to the Http Proxy. You may want to monitor a Linux server behind a firewall, and EasyHA cannot connect to the server directly, so you have to configure socks proxy. There are many free socks5 proxies. For example: 3proxy.

Here is the sample code in EasyHA:

java.net.Proxy proxy=null;
String Proxy_Authorization_headerValue=null;
if(item.getProxyId()!=null){
      final ProxyConf conf=MonitorDaemon.getInstance().getProxyConfById(item.getProxyId());
      if(conf!=null){
            SocketAddress sa=InetSocketAddress.createUnresolved(conf.getHost(), conf.getPort());
            if( conf.getProxyType().equals("http") ){
                  proxy=new Proxy(Proxy.Type.HTTP, sa);
            if(conf.getUserName()!=null&&conf.getUserName().length()>0
                  &&conf.getPassword()!=null&&conf.getPassword().length()>0){
                        Proxy_Authorization_headerValue= "Basic " + Base64.encode( (conf.getUserName()+":"+conf.getPassword()).getBytes("UTF-8")) ; 
                        }
            }else{
                  proxy=new Proxy(Proxy.Type.SOCKS, sa);
            if(conf.getUserName()!=null&&conf.getUserName().length()>0
                        &&conf.getPassword()!=null&&conf.getPassword().length()>0){
                      java.net.Authenticator authenticator = new java.net.Authenticator() {
                        @Override
                             protected java.net.PasswordAuthentication getPasswordAuthentication() {
                                  return new java.net.PasswordAuthentication(conf.getUserName(), conf.getPassword().toCharArray());
                             }
                             };
                             java.net.Authenticator.setDefault(authenticator);
                  }
            }
      }
}
if(proxy==null){
   httpUrlConn = (java.net.HttpURLConnection) url.openConnection();
}else{
   httpUrlConn = (java.net.HttpURLConnection) url.openConnection(proxy);
   if(Proxy_Authorization_headerValue!=null){
   String headerKey = "Proxy-Authorization"
   httpUrlConn.setRequestProperty(headerKey, Proxy_Authorization_headerValue); 
   }
}
if(cookie!=null){
     httpUrlConn.setRequestProperty( "cookie" , cookie);
}
httpUrlConn.setConnectTimeout(1000*3);

No comments:

Post a Comment