1 Preface
Sometimes in our program to provide access to the network using proxies, agents include HTTP, HTTPS, FTP, socks agents. For example, set up an agent in IE browser.
So we use proxies in our Java programs, like the next two ways. directly on the code.
2 Setting System Properties
Import Java.net.Authenticator;
Import java.net.PasswordAuthentication;
Import java.util.Properties;
public class ProxyDemo1 {public static void main (string[] args) {Properties prop = system.getproperties ();
Set the address of the proxy server to use for HTTP access prop.setproperty ("Http.proxyhost", "183.45.78.31");
Sets the port Prop.setproperty ("Http.proxyport", "8080") of the proxy server to use for HTTP access;
To set up a host that does not need to be accessed through a proxy server, you can use the * wildcard character and multiple addresses to separate prop.setproperty ("Http.nonproxyhosts", "localhost|192.168.0.*"); Set the proxy server address and port used for secure access it has no https.nonproxyhosts property, and it accesses Prop.setproperty ("https" according to the rules set in Http.nonproxyhosts.
ProxyHost "," 183.45.78.31 ");
Prop.setproperty ("Https.proxyport", "443");
Use the FTP proxy host, port, and host Prop.setproperty ("Ftp.proxyhost", "183.45.78.31") that do not need to use the FTP proxy server;
Prop.setproperty ("Ftp.proxyport", "21");
Prop.setproperty ("Ftp.nonproxyhosts", "localhost|192.168.0.*"); Socks Proxy server address and Port ProP.setproperty ("Socksproxyhost", "183.45.78.31");
Prop.setproperty ("Socksproxyport", "1080");
Set Login to Proxy server username and password Authenticator.setdefault (new Myauthenticator ("UserName", "Password"));
Static class Myauthenticator extends Authenticator {private String user = "";
Private String Password = "";
Public Myauthenticator (string user, string password) {this.user = user;
This.password = password; } protected Passwordauthentication Getpasswordauthentication () {return new Passwordauthenticati
On (user, Password.tochararray ()); }
}
}