Set proxy server in Java

Source: Internet
Author: User

In Java, the basic settings of the proxy server are completed by setting system properties. The proxy server is verified by setting the HTTP request header.
The following is a simple example for your reference:

// Open an HTTP Connection Based on the URL
Httpurlconnection con = (httpurlconnection) (new URL (URL). openconnection ();
If (proxy. hasproxy ()){
// Note: If proxyset is set to false, proxyhost and proxyport are still set, the proxy settings will still take effect.
// If the proxyport settings are incorrect, the proxy settings will not work.
System. getproperties (). Put ("proxyset", "true ");
System. getproperties (). Put ("proxyhost", proxy. getproxyhost ());
System. getproperties (). Put ("proxyport", String. valueof (proxy. getproxyport ()));

// If Proxy Server authentication is required, add the proxy-Authorization header to the HTTP request header,
// The format is "Basic" + ("Proxy Server user name: Password" in base64 encoding)
If (proxy. needauth ()){
Con. setrequestproperty ("proxy-Authorization", "Basic" + encoder. base64encode (proxy. getproxyuser () + ":" + proxy. getproxypass ()));
}
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/hustli/archive/2003/07/03/19349.aspx

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.