Sometimes you need to set up a proxy with Java, so how do you do it?
Http.proxyhost (default: <none>) http.proxyport (default:80 if http.proxyhost specified)
# 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
System.setproperty ("Java.net.useSystemProxies", "true");
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".
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"/>
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