Skip to main content

设置Java程序网络代理

· One min read
Alan

配置来源

To configure a Java application to send web traffic to Fiddler, set the proxy using jre:

jre -DproxySet=true -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888

Or:

jre -DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888 MyApp

Or, change the Java Virtual Machine's proxy settings programmatically:

System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "8888");
System.setProperty("https.proxyPort", "8888");
note

Apache 的 HttpClient 需要单独设置 Request 的代理:

RequestConfig config = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(3000)
.setConnectionRequestTimeout(3000)
.setProxy(new HttpHost("127.0.0.1", 3210)) // 在这里设置代理
.build();