Sometimes our network cannot be directly connected to the Internet. We need to use HTTP, https, or socket proxy to connect to the Internet. Here are some methods for connecting Java to the Internet using Proxy ,: method 1: Use System Properties to complete proxy settings. This method is relatively simple, but you cannot set proxy for individual connections:
View plainprint?
- Public static void main (string [] ARGs ){
- Properties prop = system. getproperties ();
- // Set the address of the proxy server to be used for HTTP access
- Prop. setproperty ("HTTP. proxyhost", "192.168.0.254 ");
- // Set the port for HTTP access to the proxy server to be used
- Prop. setproperty ("HTTP. proxyport", "8080 ");
- // Set the host that does not need to be accessed through the proxy server. You can use the * wildcard. Multiple addresses are separated by |.
- Prop. setproperty ("HTTP. nonproxyhosts", "localhost | 192.168.0 .*");
- // Set the proxy server address and port used for secure access
- // It does not have the HTTPS. nonproxyhosts attribute. It is accessed according to the rules set in HTTP. nonproxyhosts
- Prop. setproperty ("HTTPS. proxyhost", "192.168.0.254 ");
- Prop. setproperty ("HTTPS. proxyport", "443 ");
- // Use the FTP Proxy server host, port, and host that does not need the FTP Proxy Server
- Prop. setproperty ("ftp. proxyhost", "192.168.0.254 ");
- Prop. setproperty ("ftp. proxyport", "2121 ");
- Prop. setproperty ("ftp. nonproxyhosts", "localhost | 192.168.0 .*");
- // Socks proxy server address and port
- Prop. setproperty ("socksproxyhost", "192.168.0.254 ");
- Prop. setproperty ("Fig", "8000 ");
- // Set the username and password for logging on to the Proxy Server
- 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 passwordauthentication (user, password. tochararray ());
- }
- }
Public static void main (string [] ARGs) {<br/> properties prop = system. getproperties (); <br/> // set the address of the proxy server to be used for HTTP access <br/> prop. setproperty ("HTTP. proxyhost "," 192.168.0.254 "); <br/> // set the port of the proxy server to be accessed by HTTP <br/> prop. setproperty ("HTTP. proxyport "," 8080 "); <br/> // you can use the * wildcard to specify the host that does not need to be accessed by the proxy server, separate multiple addresses with | <br/> prop. setproperty ("HTTP. nonproxyhosts "," localhost | 192.168.0. * "); <br/> // set the proxy server address and port used for secure access <br/> // It does not have HTTPS. nonproxyhosts attribute, which follows HTTP. access rule set in nonproxyhosts <br/> prop. setproperty ("HTTPS. proxyhost "," 192.168.0.254 "); <br/> prop. setproperty ("HTTPS. proxyport "," 443 "); <br/> // use the FTP Proxy server host, port, and host that does not need the FTP Proxy server <br/> prop. setproperty ("FTP. proxyhost "," 192.168.0.254 "); <br/> prop. setproperty ("FTP. proxyport "," 2121 "); <br/> prop. setproperty ("FTP. nonproxyhosts "," localhost | 192.168.0. * "); <br/> // address and port of the socks proxy server <br/> prop. setproperty ("socksproxyhost", "192.168.0.254"); <br/> prop. setproperty ("socksproxyport", "8000"); <br/> // set the username and password used to log on to the proxy server <br/> authenticator. setdefault (New myauthenticator ("username", "password ")); <br/>}< br/> static class myauthenticator extends authenticator {<br/> private string user = ""; <br/> private string Password = ""; <br/> Public myauthenticator (string user, string password) {<br/> This. user = user; <br/> This. password = password; <br/>}< br/> protected passwordauthentication getpasswordauthentication () {<br/> return New passwordauthentication (user, password. tochararray (); <br/>}< br/>}Method 2 proxy is used to implement proxy for each connection. This method can only be used in JDK 1.5 or later versions (including JDK). The advantage is that the proxy for each connection can be set separately, the disadvantage is that it is difficult to set: View plainprint?
- Public static void main (string [] ARGs ){
- Try {
- URL url = new URL ("http://www.baidu.com ");
- // Create a Proxy Server
- Inetsocketaddress ADDR = new inetsocketaddress ("192.168.0.254 ",
- 8080 );
- // Proxy = new proxy (proxy. type. Socks, ADDR); // socket proxy
- Proxy proxy = new proxy (proxy. type. HTTP, ADDR); // HTTP Proxy
- // If we know the name of the proxy server, you can directly use
- // End
- Urlconnection conn = URL. openconnection (proxy );
- Inputstream in = conn. getinputstream ();
- // Inputstream in = URL. openstream ();
- String S = ioutils. tostring (in );
- System. Out. println (s );
- } Catch (exception e ){
- E. printstacktrace ();
- }
- }