Java How to set proxy server,s cancel Agent erver

Source: Internet
Author: User
Tags base64

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

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.