The use of j2se5.0 new features proxy server

Source: Internet
Author: User
Tags ftp

j2se1.5 is also known as the j2se5.0, the proxy server settings before 5.0 is very unfriendly, now, 5.0 provides a more flexible control over the proxy server, it can be set on the Http,https,ftp,socks, You can also set up hosts and addresses that do not need to be passed through a proxy server. This is similar to the proxy server we set up in IE and Firefox.

1. You can set it up in a java-dxxxx=yyyy way at the start of the program, you can also put settings into the system properties in the program, you can also set the proxy class, through which to control.

2. You can do the load balancing of your proxy server by Proxyselector.

package com.kuaff.jdk5package;


import java.io.IOException;


import Java.io.InputStream;


import java.net.InetSocketAddress;


import java.net.MalformedURLException;


import Java.net.Proxy;


import Java.net.ProxySelector;


import java.net.SocketAddress;


import Java.net.URI;


import java.net.URISyntaxException;


import Java.net.URL;


import java.net.URLConnection;


import java.util.List;


import java.util.Properties;


public class NetProxy


{


//Test the network default configuration of the local JVM


public void Setlocalproxy ()


{


Properties prop = System.getproperties ();


//Set the address of the proxy server to use for HTTP access


prop.setproperty ("Http.proxyhost", "10.10.0.96");


//Set the port
HTTP Access Proxy server to use

prop.setproperty ("Http.proxyport", "8080");


//Set of hosts that do not need to be accessed through a proxy server, you can use the * wildcard character and multiple addresses to separate the


prop.setproperty ("Http.nonproxyhosts", "localhost|10.10.*");


//Set the proxy server address and port used for secure access


//It does not have a https.nonproxyhosts property, which is accessed according to the rules set in Http.nonproxyhosts


prop.setproperty ("Https.proxyhost", "10.10.0.96");


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", "10.10.0.96");


prop.setproperty ("Ftp.proxyport", "2121");


prop.setproperty ("Ftp.nonproxyhosts", "localhost|10.10.*");


//socks Proxy server address and port


Prop.setproperty ("Socksproxyhost", "10.10.0.96");


prop.setproperty ("Socksproxyport", "1080");


}


//Clear proxy settings


public void Removelocalproxy ()


{


Properties prop = System.getproperties ();


prop.remove ("Http.proxyhost");


prop.remove ("Http.proxyport");


prop.remove ("http.nonproxyhosts");


prop.remove ("Https.proxyhost");


prop.remove ("Https.proxyport");


prop.remove ("Ftp.proxyhost");


prop.remove ("Ftp.proxyport");


prop.remove ("ftp.nonproxyhosts");


prop.remove ("Socksproxyhost");


prop.remove ("Socksproxyport");


}


//


//Test HTTP


public void Showhttpproxy (Object ... proxy)


{


URL url = null;


Try


{


url = new URL ("Http://blog.csdn.com/smallnest");


}


catch (malformedurlexception e)


{


return;


}


Try


{


URLConnection conn = null;


switch (proxy.length)


{


Case 0:


conn = Url.openconnection ();


break;


Case 1:


conn = url.openconnection ((Proxy) proxy[0]);


break;


Default:


break;


}


if (conn = null)


return;


Conn.setconnecttimeout (3000); Set Connection Timeout


InputStream in = Conn.getinputstream ();


byte[] b = new byte[1024];


Try


{


while (In.read (b) > 0)


{


System.out.println (New String (b));


}


}


catch (IOException E1)


{


}


}


catch (IOException E1)


{


E1.printstacktrace ();


}


}


//Test FTP


public void Showftpproxy (Object ... proxy)


{


URL url = null;


Try


{


url = new URL ("ftp://ftp.tsinghua.edu.cn");


}


catch (malformedurlexception e)


{


return;


}


Try


{


URLConnection conn = null;


switch (proxy.length)


{


Case 0:


conn = Url.openconnection ();


break;


Case 1:


conn = url.openconnection ((Proxy) proxy[0]);


break;


Default:


break;


}


if (conn = null)


return;


Conn.setconnecttimeout (3000); Set Connection Timeout


InputStream in = Conn.getinputstream ();


byte[] b = new byte[1024];


Try


{


while (In.read (b) > 0)


{


System.out.println (New String (b));


}


}


catch (IOException E1)


{


}


}


catch (IOException E1)


{


E1.printstacktrace ();


}


}


//Get a proxy


public Proxy getproxy (Proxy.type Type, String host, int port)


{


socketaddress addr = new inetsocketaddress (host,port);


Proxy typeproxy = new Proxy (type, addr);


return typeproxy;


}


public static void Main (string[] args)


{


NetProxy proxy = new NetProxy ();


//test proxy Server


Proxy.setlocalproxy ();


Proxy.showhttpproxy ();


//The following two lines are to clear the system properties and specify proxy servers through the proxy class


//Proxy.removelocalproxy


//proxy.showhttpproxy (Proxy.getproxy (Proxy.Type.SOCKS, "10.10.0.96", 1080));


}


}

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.