Various Java proxy settings

Source: Internet
Author: User

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?
  1. Public static void main (string [] ARGs ){
  2. Properties prop = system. getproperties ();
  3. // Set the address of the proxy server to be used for HTTP access
  4. Prop. setproperty ("HTTP. proxyhost", "192.168.0.254 ");
  5. // Set the port for HTTP access to the proxy server to be used
  6. Prop. setproperty ("HTTP. proxyport", "8080 ");
  7. // Set the host that does not need to be accessed through the proxy server. You can use the * wildcard. Multiple addresses are separated by |.
  8. Prop. setproperty ("HTTP. nonproxyhosts", "localhost | 192.168.0 .*");
  9. // Set the proxy server address and port used for secure access
  10. // It does not have the HTTPS. nonproxyhosts attribute. It is accessed according to the rules set in HTTP. nonproxyhosts
  11. Prop. setproperty ("HTTPS. proxyhost", "192.168.0.254 ");
  12. Prop. setproperty ("HTTPS. proxyport", "443 ");
  13. // Use the FTP Proxy server host, port, and host that does not need the FTP Proxy Server
  14. Prop. setproperty ("ftp. proxyhost", "192.168.0.254 ");
  15. Prop. setproperty ("ftp. proxyport", "2121 ");
  16. Prop. setproperty ("ftp. nonproxyhosts", "localhost | 192.168.0 .*");
  17. // Socks proxy server address and port
  18. Prop. setproperty ("socksproxyhost", "192.168.0.254 ");
  19. Prop. setproperty ("Fig", "8000 ");
  20. // Set the username and password for logging on to the Proxy Server
  21. Authenticator. setdefault (New myauthenticator ("username", "password "));
  22. }
  23. Static class myauthenticator extends authenticator {
  24. Private string user = "";
  25. Private string Password = "";
  26. Public myauthenticator (string user, string password ){
  27. This. User = user;
  28. This. Password = password;
  29. }
  30. Protected passwordauthentication getpasswordauthentication (){
  31. Return new passwordauthentication (user, password. tochararray ());
  32. }
  33. }

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?

  1. Public static void main (string [] ARGs ){
  2. Try {
  3. URL url = new URL ("http://www.baidu.com ");
  4. // Create a Proxy Server
  5. Inetsocketaddress ADDR = new inetsocketaddress ("192.168.0.254 ",
  6. 8080 );
  7. // Proxy = new proxy (proxy. type. Socks, ADDR); // socket proxy
  8. Proxy proxy = new proxy (proxy. type. HTTP, ADDR); // HTTP Proxy
  9. // If we know the name of the proxy server, you can directly use
  10. // End
  11. Urlconnection conn = URL. openconnection (proxy );
  12. Inputstream in = conn. getinputstream ();
  13. // Inputstream in = URL. openstream ();
  14. String S = ioutils. tostring (in );
  15. System. Out. println (s );
  16. } Catch (exception e ){
  17. E. printstacktrace ();
  18. }
  19. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.