JDK 使用代理
-D
选项
java -Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080 -Dhttp.nonProxyHosts="localhost|host.example.com" test.jar
http.proxyHost
http.proxyPort
http.nonProxyHosts: localhost|192.168.0.*
https.proxyHost: 192.168.0.254
https.proxyPort: 2121
http.nonProxyHosts (the HTTPS protocol handler will use the same as the http handler)
ftp.proxHost
ftp.proxyPort
ftp.nonProxyHosts
socksProxyHost
socksProxyPort (without dot. This is for historical reasons and to ensure backward compatibility. default port is 1080)
- code-nestd
// 设置代理
System.setProperty("http.proxyHost", "webcache.example.com");
System.setProperty("http.proxyPort", "8080");// 下一个连接将会使用代理
URL url = new URL("http://java.example.org/");
InputStream in = url.openStream();// 清除代理
System.clearProperty("http.proxyHost");// 从现在开始,http 连接将直接完成而不再使用代理
HttpClient (apache 使用代理)
import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
...
HttpHost proxy = new HttpHost("www.proxy.com", 8080, "http");
HttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build();