1. A Brief introduction
Proxy Serveris an important server security feature that works primarily in the session layer of the Open Systems Interconnection (OSI) model, and thus acts as a firewall .
The
Proxy server is mostly used to connect Internet (Internet) and intranet ( LAN ). Main is the agent network users to obtain network information. The image says, it is the intermediary of the network information. This article focuses on how to set up Proxy server with Java code.
The security of the primary server is obvious in order to prevent the user from being attacked by a pseudo-user, allowing users to access the primary server through Proxy server. A diagram understanding Proxy server and its role
1. FunctionHow to set up proxy server in Java, very easy
public void Setserviceproxy (config config) {this.config = config; System.setproperty ("Http.proxyhost", Config.getproxyhost ()); System.setproperty ("Http.proxyport", Config.getproxyport ()); System.setproperty ("Https.proxyhost", Config.getproxyhost ()); System.setproperty ("Https.proxyport", Config.getproxyport ());}
Then, in each httpurlconnection request, set the agent Serverusername and password. and Base64 encryption (see your proxy server requirements, may vary depending on the proxy server). Code such as the following
Private HttpURLConnection Getproxyhttpconnection () throws IOException {String authorization = Config.getproxyuser () + ": "+ Config.getproxypassword (); URL url = new URL (config.getrequesturl ()); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setrequestproperty ("Proxy-Authorization", " Basic "+ base64.encodebase64string (Authorization.getbytes ())); return conn;}
BASE64 encryption tool. It's in Apache's common bag. If you can't find it, download it from here http://commons.apache.org/proper/commons-codec/download_codec.cgiLet's say you want to cancel the proxy server settings, and it's easy, code such as the following
public void Removeserviceproxy () {system.getproperties (). Remove ("Http.proxyhost"); System.getproperties (). Remove ("Http.proxyport"); System.getproperties (). Remove ("Https.proxyhost"); System.getproperties (). Remove ("Https.proxyport");
3. SummaryProxy Server is designed for the security, stability, and reliability of the primary server, and it is also a common thing that Java developers must have. If you're studying this, just try it.
reprint marked out office, 2014 6 one months Wang Jing end
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Java How to set proxy server,s cancel Agent erver