In Java, the basic settings of the proxy server are completed by setting system properties. The proxy server is verified by setting the HTTP request header.
The following is a simple example for your reference:
// Open an HTTP Connection Based on the URL
Httpurlconnection con = (httpurlconnection) (new URL (URL). openconnection ();
If (proxy. hasproxy ()){
// Note: If proxyset is set to false, proxyhost and proxyport are still set, the proxy settings will still take effect.
// If the proxyport settings are incorrect, the proxy settings will not work.
System. getproperties (). Put ("proxyset", "true ");
System. getproperties (). Put ("proxyhost", proxy. getproxyhost ());
System. getproperties (). Put ("proxyport", String. valueof (proxy. getproxyport ()));
// If Proxy Server authentication is required, add the proxy-Authorization header to the HTTP request header,
// The format is "Basic" + ("Proxy Server user name: Password" in base64 encoding)
If (proxy. needauth ()){
Con. setrequestproperty ("proxy-Authorization", "Basic" + encoder. base64encode (proxy. getproxyuser () + ":" + proxy. getproxypass ()));
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/hustli/archive/2003/07/03/19349.aspx