Common proxy settings for Java

Source: Internet
Author: User

As the company on the Internet to implement agency mechanism,
And recently, I've been studying OPENAPI on the web.
There is no way to use the agent, I previously an article introduced the HttpClient proxy usage,
This article describes how to use the proxy for basic java.
The most commonly used global configuration agent.

Java code
  1. Properties prop = System.getproperties ();
  2. //HTTP proxy IP Settings
  3. Prop.setproperty ("Http.proxyhost", "10.28.0.254");
  4. //HTTP proxy port settings
  5. Prop.setproperty ("Http.proxyport", "80");
  6. //You can also set the address that does not need to use the proxy
  7. Prop.setproperty ("http.nonproxyhosts", "localhost|10.28.0.*");
  8. //Set the proxy server address and port for HTTPS secure access
  9. Prop.setproperty ("Https.proxyhost", "10.28.0.254");
  10. Prop.setproperty ("Https.proxyport", "443");
  11. The filter address property for secure access is also http.nonproxyhosts and not https.nonproxyhosts
  12. //ftp proxy settings into the next
  13. Prop.setproperty ("Ftp.proxyhost", "192.168.0.254");
  14. Prop.setproperty ("Ftp.proxyport", "2121");
  15. Prop.setproperty ("ftp.nonproxyhosts", "localhost|192.168.0.*");
  16. proxy settings for//socks
  17. Prop.setproperty ("Socksproxyhost", "192.168.0.254");
  18. Prop.setproperty ("Socksproxyport", "8000");


Sometimes proxies need to be authenticated at this point we need to define a class that inherits the class authenticator.

Java code
  1. Public class Myauthenticator extends Authenticator {
  2. private String username = "";
  3. private String Password = "";
  4. Public Myauthenticator (string Username, string password) {
  5. this.username = Username;
  6. This.password = password;
  7. }
  8. protected Passwordauthentication getpasswordauthentication () {
  9. Returnnew passwordauthentication (username, Password.tochararray ());
  10. }
  11. }
  12. Set the user name and password to log on to the proxy server
  13. Authenticator.setdefault (new Myauthenticator ("UserName", "Password"));


Of course, this method can meet most of the requirements but there are some limitations,
Is that all connections are configured by proxy with a unified attribute and cannot be configured for a specific connection.

JDK5 and later releases introduce a new proxy configuration that can be configured for a specific connection

Java code
    1. URL url = new URL ("http://www.shanhe114.com");
    2. //Create Proxy Server
    3. Inetsocketaddress addr = new Inetsocketaddress ("10.28.0.4",
    4. 8080);
    5. //proxy proxy = new Proxy (Proxy.Type.SOCKS, addr);//socks Agent
    6. Proxy proxy = new Proxy (Proxy.Type.HTTP, addr); //http Agent
    7. //Other ways to see Proxy.type properties
    8. URLConnection conn = url.openconnection (proxy);
    9. InputStream in = Conn.getinputstream ();
    10. //inputstream in = Url.openstream ();
    11. String content = ioutils.tostring (in);
    12. SYSTEM.OUT.PRINTLN (content);

Common proxy settings for Java

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.