How to set up proxies in Java

Source: Internet
Author: User
Tags ftp 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 Prop.setproperty ("https" according to the rules set in Http.nonproxyhosts.
        ProxyHost "," 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 Passwordauthenticati
        On (user, Password.tochararray ()); }
    }
     
}

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.