Several methods of using proxy access network in Java __java

Source: Internet
Author: User
Tags static class

Sometimes our network can not directly connect to the extranet, you need to use HTTP or HTTPS or the socket agent to connect to the extranet, here is the Java use Agent to connect to the extranet some of the methods, I hope that your program useful.

Method 1: using System Properties to complete proxy settings is simpler, but you cannot set the proxy on a separate connection:

      public static void Main (string[] args) {Properties prop = system.getproperties ();//Set HTT P Access the address of the proxy server to use Prop.setproperty ("Http.proxyhost", "192.168.0.254"); 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.proxyhost" in accordance with the rules set in Http.nonproxyhosts. "," 192.168.0.254 "); Prop.setproperty ("Https.proxyport", "443"); Use the FTP proxy host, port, and host Prop.setproperty ("Ftp.proxyhost", "192.168.0.254") that do not need to use the FTP proxy server; 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 ("Socksproxyport", "8000"); 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 Passwordaut Hentication getpasswordauthentication () {returnnew passwordauthentication (user, Password.tochararray ());}  

Method 2: use proxy to implement the proxy for each connection, this method can only be used in the version of JDK 1.5 or more (including jdk1.5), the advantage is that each connection can be set independently of the agent, the disadvantage is to set the more cumbersome:

public static void Main (string[] args) {try {URL url = new URL ("http://www.baidu.com");//Create Proxy Server inetsocketaddress add R = 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//If we know the name of the proxy server, you can use//end URLConnection conn = Url.openconnection (proxy) directly; InputStream in = Conn.getinputstream (); InputStream in = Url.openstream (); String s = ioutils.tostring (in); System.out.println (s); catch (Exception e) {e.printstacktrace ();}}


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.