Several methods of using proxy access network in Java
Source: Internet
Author: User
Http://javasky.bloghome.cn/posts/86087.html sometimes our network can not be directly connected to the extranet, you need to use HTTP or HTTPS or the socket agent to connect to the extranet, this is the Java use of proxy to connect to the outside Network of some methods, I hope to be useful for your program.
Method using System Properties to complete proxy settings is simpler, but you cannot set the proxy for a separate connection:/** *
@paramArgs//** *
@paramargs *
PublicStaticvoid Main (string[] args) { Properties prop = System.getproperties (); //Set the address of the proxy server to use for HTTP access Prop.setproperty ("Http.proxyhost", "192.168.0.254"); //Set the port of the proxy server to use for HTTP access Prop.setproperty ("Http.proxyport", "8080"); //Set up a host that does not need to be accessed through a proxy server, you can use the * wildcard character, multiple addresses with | Separation prop.setproperty ("Http.nonproxyhosts", "localhost|192.168.0.*"); //Set the proxy server address and port used for secure access //It is not Https.nonproxyhosts property, which accesses prop.setproperty according to the rules set in Http.nonproxyhosts ( "Https.proxyhost", "192.168.0.254"); prop.setproperty ("Https.proxyport", "443"); //Using the FTP proxy hosts, ports, and hosts that do not need to use the FTP proxy server Prop.setproperty ("Ftp.proxyhost", "192.168.0.254"); 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 user name and password for login to proxy server Authenticator.setdefault (
NewMyauthenticator ("UserName", "Password")); }
StaticclassMyauthenticator
extendsAuthenticator {
PrivateString user = "";
PrivateString password = "";
PublicMyauthenticator (string user, string password) {
This. user = user;
This. Password = password; }
protectedPasswordauthentication getpasswordauthentication () {
returnNewPasswordauthentication (user, password. ToCharArray ()); The method two uses proxy to implement a proxy for each connection, which can only be used in versions above JDK 1.5 (including jdk1.5), with the advantage that it is possible to set up the proxy for each connection individually, with the disadvantage of setting up more cumbersome:
PublicStaticvoidMain (string[] args) {
Try{URL url =
NewURL ("http://www.baidu.com"); Create Proxy server Inetsocketaddress addr =
NewInetsocketaddress ("192.168.0.254", 8080); Proxy proxy = new Proxy (Proxy.Type.SOCKS, addr); Socket Agent Proxy proxy =
NewProxy (Proxy.type. HTTP, addr); HTTP Proxy//If we know the name of the proxy server, you can use//end URLConnection conn = Url.openconnecti On (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, what is the problem also hope everyone corrected ================================/*
* Output:
*
*/
ImportJava.io.InputStream;
ImportJava.net.URL;
Public classMainClass {
Public StaticvoidMain (String args[]) {
Try{
URL url =
NewURL ("http://www.java2s.com");
Obtain output stream
InputStream is = Url.openstream ();
Read and display data from URL
byteBuffer[] =
Newbyte[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
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.