The basic settings of the proxy server in Java are done by setting the system properties. The verification of the proxy server is done by setting an HTTP request header.
The following is a simple example for your reference:
/ / Open HTTP connection according to address URL
HttpurlConnection Con = (HTTPURLCONNECTION) (New URL (URL)) .openConnection ();
IF (proxy.hasproxy ()) {
// Note: If ProxySet is false, ProxyHost and ProxyPort are still set, and the proxy settings still work.
// If ProxyPort is set, the proxy setting will not work.
System.getProperties (). Put ("Proxyset", "True");
System.getProperties (). Put ("proxyhost", proxy.getproxyhost ());
System.getProperties (). Put ("proxyport", string.valueof (proxy.getproxyport ())))
// If you need a proxy server verification, add the proxy-authorization header in the HTTP request header.
// Format is: "Basic" ("Proxy Server User Name: Password" Base64 Code)
IF (proxy.needauth ()) {
Con.SetRequestProperty ("Proxy-Authorization", "Basic" Encoder.Base64Encode (Proxy.getProxyUser () ":" Proxy.getProxyPass ()));
}
}
-
http://www.javaidea.net