JAVA set proxy mode

Using Java 's HttpURLConnection class can implement the functions of HttpClient without relying on any other class library. However, if the website you need to visit is blocked, the HttpURLConnection class will have a connection timeout error. At this time, you need to set up a proxy for him.

 

There are two ways to set a proxy (Proxy):

1. By setting system properties (System.setPropery(String key, String value)

First you can see the properties supported by Java here . We can use the http.proxyHost and http.proxyPort properties. As the name implies, it is to set the proxy server address and proxy port respectively.

// Set the properties before you make the Http request  
System.setProperty("http.proxyHost", "www.proxy.com");  
System.setProperty("http.proxyPort", "8080");

Replace the above www.proxy.com with your proxy server address or IP address, and the corresponding port with the real port, Http connection and can work. It should be noted that if you set these properties, then all Http requests will go through the proxy server. These properties are at the JVM level and are valid for all similar requests after they are set. For example, the above is about http, as well as about ftp and so on.

 

If your proxy server doesn't require authentication, that's it. But generally it needs to be verified. But if you look at the list of properties supported by Java above , you will find that there is nothing in there that you expect

http.proxyUserName=username  
http.proxyPassword=password  

these two properties. At this time, the java.net.Authenticator class is needed to complete the general Http authentication. But the java.net.Authenticator class is an abstract class, and we need to instantiate our own class if we want to use it.

public class BasicAuthenticator extends Authenticator {  
    String userName;  
    String password;  
  
    public BasicAuthenticator(String userName, String password) {  
        this.userName = userName;  
        this.password = password;  
    }  
  
    /**
     * Called when password authorization is needed.  Subclasses should
     * override the default implementation, which returns null.
     *
     * @return The PasswordAuthentication collected from the
     *         user, or null if none is provided.
     */  
    @Override  
    protected PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication(userName, password.toCharArray());  
    }  
}  

We need to override the getPasswordAuthentication() method of the Java .NET .Authenticator class and return a PasswordAuthentication instance. To make it work, you also need to set

Authenticator.setDefault(new BasicAuthenticator(userName, password));  

In this way, Http Basic-based authentication is provided, and then the proxy that needs authentication can be used smoothly.

 

2, through the java.Net.Proxy class.

This way is to instantiate a Proxy class to provide proxy server information, such as port and address.

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));  
URLConnection conn = url.openConnection(proxy);  

The way to use a proxy is to pass a Proxy parameter when opening an Http connection. If authentication information is required, we can add an Http header parameter to achieve this.

// The format is as follows:  
"Proxy-Authorization"= "Basic Base64.encode(user:password)"  
String headerKey = "Proxy-Authorization";  
String headerValue = "Basic " + Base64.encode(user+":"+password);  
conn.setRequestProperty(headerKey, headerValue);  
  
//..........

Among them, Base64.encode(user:password) refers to using the Base64-encoded value as part of the value after concatenating the username and password with a colon.  

In this way only specific Http connections are affected, but the code needs to be modified. Whether Authenticator can be used in this way has not been verified yet.

 

http://blog.csdn.net/u013782203/article/details/51790460

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326428185&siteId=291194637