Share two ways to set up proxies in Java _java

Source: Internet
Author: User
Tags ord static class

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 the Prop.setproperty ("Https.proxyhos" in accordance with the rules set in Http.nonproxyhosts.
    T "," 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 passwordauthentication (user, PASSW
    Ord.tochararray ()); }
  }
 
}

3 using proxy

Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.net.Authenticator;
Import java.net.HttpURLConnection;
Import java.net.InetSocketAddress;
Import java.net.PasswordAuthentication;
Import Java.net.Proxy;
 
Import Java.net.URL; public class ProxyDemo2 {public static void main (string[] args) throws Exception {URL url = new URL ("http://www.3l
    Ai8.com ");
    /create proxy server inetsocketaddress addr = new Inetsocketaddress ("192.168.0.254", 8080); Proxy proxy = new Proxy (Proxy.Type.SOCKS, addr); Socket Agent proxy = new Proxy (Proxy.Type.HTTP, addr); HTTP proxy Authenticator.setdefault (new Myauthenticator ("username", "password");/set user and password for proxy httpurlconnection c Onnection = (httpurlconnection) url.openconnection (proxy);/set proxy access InputStreamReader in = new InputStreamReader (conne
    Ction.getinputstream ());
    BufferedReader reader = new BufferedReader (in);
      while (true) {String s = reader.readline ();
   if (s!= null) {     System.out.println (s);
    }} 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, PASSW
    Ord.tochararray ()); }
  }
 
}

4 Summary

OK, so simple, done, with the first way is a global agent, in the same way can be specific to the use of the agent. Knowing this, we can do what we want to do.

Related Article

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.