Several methods for using proxy to access the network in Java

Source: Internet
Author: User
Http://javasky.bloghome.cn/posts/86087.html

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, so we hope it will be useful to your program.
Method 1: Use System Properties to complete proxy settings. This method is relatively simple, but you cannot set proxy for individual connections :/*** @ ParamARGs *//*** @ ParamARGs */ Public Static VoidMain (string [] ARGs) {properties prop = system. Getproperties(); // Set the address of the proxy server to be used for HTTP access prop. setproperty ("HTTP. proxyhost "," 192.168.0.254 "); // set the port prop for HTTP access to the proxy server to be used. setproperty ("HTTP. proxyport "," 8080 "); // sets the host that does not need to be accessed through the proxy server. You can use the * wildcard. Multiple addresses are separated by | prop. setproperty ("HTTP. nonproxyhosts "," localhost | 192.168.0. * "); // set the proxy server address and port used for secure access // It does not have HTTPS. nonproxyhosts attribute, which follows HTTP. the rules set in nonproxyhosts access prop. setproperty ("HTTPS. proxyhost "," 192.168.0.254 "); prop. setproperty ("HTTPS. proxyport "," 443 "); // use the FTP Proxy server host, port, and host prop that does not need the FTP proxy server. setproperty ("FTP. proxyhost "," 192.168.0.254 "); prop. setproperty ("FTP. proxyport "," 2121 "); prop. setproperty ("FTP. nonproxyhosts "," localhost | 192.168.0. * "); // address and port of the socks Proxy Server prop. setproperty ("socksproxyhost", "192.168.0.254"); prop. setproperty ("socksproxyport", "8000"); // set the username and password authenticator used to log on to the proxy server. Setdefault( NewMyauthenticator ("username", "password "));} Static ClassMyauthenticator ExtendsAuthenticator { PrivateString user = ""; PrivateString Password = ""; PublicMyauthenticator (string user, string password ){ This. User = user; This. Password = password ;} ProtectedPasswordauthentication getpasswordauthentication (){ Return NewPasswordauthentication (user, password. tochararray () ;}} 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 you can set the proxy for each connection separately. The disadvantage is that it is difficult to set the Proxy: Public Static VoidMain (string [] ARGs ){ Try{URL url = NewURL ("http://www.baidu.com"); // create Proxy Server inetsocketaddress ADDR = NewInetsocketaddress ("192.168.0.254", 8080); // proxy = new proxy (proxy. type. Socks, ADDR); // socket proxy = NewProxy (proxy. type. HTTP, ADDR); // HTTP Proxy // if we know the name of the proxy server, you can directly use // end urlconnection conn = URL. openconnection (proxy); inputstream in = Conn. getinputstream (); // inputstream in = URL. openstream (); string S = ioutils. Tostring(IN); system. Out. Println (s );} Catch(Exception e) {e. printstacktrace () ;}} is not very detailed, for more information, see =========================================/ *
* Output:
*
*/

ImportJava. Io. inputstream;
ImportJava.net. url;

Public classMainclass {

Public static VoidMain (string ARGs []) {
Try{

URL url = NewURL ("http://www.java2s.com ");

// Obtain output stream
Inputstream is = URL. openstream ();

// Read and display data from URL
ByteBuffer [] = New Byte[1024];
IntI;
While(I = is. Read (buffer ))! =-1 ){
System. Out. Write (buffer, 0, I );
}
} Catch(Exception e ){
E. printstacktrace ();
}
}
} Http://www.java2s.com/Code/JavaAPI/java.net/URLopenStream.htm

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.