Java Network Proxy Settings notes

Source: Internet
Author: User


Sometimes you need to set up a proxy with Java, so how do you do it?


    • Use System.setproperty (...)

Http.proxyhost (default: <none>) http.proxyport (default:80 if http.proxyhost specified)


    • Using Jvmargs

# Specify the appropriate propertyjava-dhttp.proxyhost=your_proxy_host-dhttp.proxyport=proxy_port_number at startup ... # Selenium also supports specifying username and password, is it supported in pure Java? Java-dhttp.proxyhost=myproxy.com-dhttp.proxyport=1234-dhttp.proxyuser=username-dhttp.proxypassword=example-jar Selenium-server.jar


    • Using the system default proxy

System.setproperty ("Java.net.useSystemProxies", "true");


    • Using the proxy class to set parameters

Create proxy instance proxy IP address =127.0.0.1 port =8087proxy proxy = new Proxy (Proxy.Type.HTTP, New inetsocketaddress ("127.0.0.1", 8087 )); URL url = new URL ("http://www.google.com"); HttpURLConnection UC = (httpurlconnection) url.openconnection (proxy); Uc.connect ();



Note To access a resource without a proxy, how do I set it?

Do not use proxy url url = new URL for a resource ("http://internal.server.local/"); URLConnection conn = url.openconnection (proxy.no_proxy);
# You can also use Http.nonproxyhosts (default: <none>) http.nonproxyhosts indicates the hosts which should be connected too dire Ctly and not through the proxy server. The value can be a list of hosts, each seperated by a |, and in addition a wildcard character (*) can is used for matching . For example:-dhttp.nonproxyhosts= "*.foo.com|localhost".


    • Using third-party packages such as Apache's HttpClient package

Defaulthttpclient httpclient = new Defaulthttpclient (); Httphost proxy = new Httphost ("Someproxy", 8080); Httpclient.getparams (). Setparameter (Connroutepnames.default_proxy, proxy);


What the hell is "suspect" Proxyselector?

Depending on the URL to decide which proxy to access resources (for example, HTTP with 127.0.0.1:8080 access; FTP with 127.0.0.1:3128 access, etc.)

public void Foo () throws URISyntaxException {///Open system default proxy//(because the system proxy settings are used here to illustrate how Proxyselector chooses proxies based on different URLs) S        Ystem.setproperty ("Java.net.useSystemProxies", "true");    Proxyselector PS = Proxyselector.getdefault ();    list<proxy> lst = null; For (URI uri:new uri[]{new uri ("ftp://ftpsite.com"), new Uri ("Http://httpsite.com"), new Uri ("Https://http        Ssite.com ")}) {lst = Ps.select (URI);    SYSTEM.OUT.PRINTLN (LST); }}


Execution result ():

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/DA/wKioL1TtbYuyYQ5XAAIVU5aT2wA384.jpg "title=" 1.jpg " alt= "Wkiol1ttbyuyyq5xaaivu5at2wa384.jpg"/>



    • More detailed instructions (if required for username and password verification)

Reference:

Http://www.rgagnon.com/javadetails/java-0085.html

Http://docs.oracle.com/javase/6/docs/technotes/guides/net/properties.html


Java Network Proxy Settings notes

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.