J2SE5.0 New Features Use Proxy Server

xiaoxiao2021-03-06  54

5.0 Previous proxy server settings are quite unfriendly, now 5.0 provides a more flexible control of the proxy server, which can be set separately for HTTP, HTTPS, FTP, SOCKS, etc., and can also set the host of the proxy server. And address. This is similar to us to set the proxy server in IE, Firefox. 1. You can set up when you start with Java -Dxxxx = YYYY mode, you can also set it in the system properties in the program, you can also set up the Proxy class, control it through it. 2. You can use the ProxySelector to do your own proxy server load balancing. 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 Local JVM network default configuration public void setlocalProxy () {Properties prop = system.getProperties (); // Set HTTP access to the address of the proxy server to use Prop.SetProperty ("http.proxyhost" , "10.10.96"); // Set HTTP access to the port prop.SetProperty ("http.proxyport", "8080") of the proxy server you want to use; // Settings You do not need to access the host through the proxy server, you can use * Wildcard, multiple addresses | Separate Prop.SetProperty ("http.nonproxyhosts", "localhost | 10.10. *");

/ / Set the security access to the proxy server address and port // it does not have an HTTPS.NONPROXYHOSTS attribute, which is accessed by the rules set in http.nonproxyhosts to access Prop.SetProperty, "10.0.96"); PROP. SetProperty ("https.proxyport", "443");

// Host, ports, ports, ports, ports, and do not need to use the FTP proxy server, "10.0.0.0.0.0.0.0.0.0.0.0.0.0.", "ftp.proxyport", "2121") "Prop.SetProperty (" ftp.nonproxyhosts "," localhost | 10.10. * ");

// SOCKS proxy server address and port prop.setProperty ("SocksProxyHost", "10.0.96"); Prop.SetProperty ("SocksProxyport", "1080");} // Clear Proxy Setup PUBLIC VOID RemovelProxy () {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 showhtpppproxy (object ... proxy) {url url = null; try {url = new url ("http://blog.9cbs.com/smallnest");} catch (MalforMedurLexcection E) {Return } Try {urlConnection conn = null; switch (proxy.Length) {case 0: conn = url.openconnection (); break; cas 1: conn = url.openconnection ((proxy) proxy [0]); break; default : Break;

IF (conn == null) return;

Conn.setConnectTimeout (3000); // Setting the connection timeout time inputStream in = conn.getinputStream (); byte [] b = new byte [1024]; try {while (in.ready (in.out (b)> 0) {system.out .println (new string (b));}} catch (ooException e1) {}} catch (ooException 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; cas 1: conn = url.openconnection ((proxy) proxy [0]); break; default : Break;

IF (conn == null) return;

Conn.setConnectTimeout (3000); // Setting the connection timeout time inputStream in = conn.getinputStream (); byte [] b = new byte [1024]; try {while (in.ready (in.out (b)> 0) {system.out .println (new string (b));}} catch (ooException e1) {}} catch (ooException 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 clear system properties, and specify the proxy server through the Proxy class // proxy.removelocalproxy //proxy.showhttpproxy(Proxy.getProxy(Proxy.Type.socks, "10.10.96 ", 1080);}}

转载请注明原文地址:https://www.9cbs.com/read-82433.html

New Post(0)