java 使用 org.apache.commons.httpclient 时,我可以在命令行上设置代理吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4324089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I set the proxy on the command line when using org.apache.commons.httpclient?
提问by The Archetypal Paul
If an application uses the java.net.* routines, I can set a proxy when invoking the application like this:
如果应用程序使用 java.net.* 例程,我可以在调用应用程序时设置代理,如下所示:
java -Dhttp.proxyHost=proxy.server.com -Dhttp.proxyPort=8000 <whatever-the-app-is>
However, I have an application (which I can't change) using org.apache.commons.httpclient to do the http communication. It doesn't specify a procxy server, but it does use the default HttpConnection. Is there some way I can tell the apache http client from the command line to use a proxy server?
但是,我有一个应用程序(我无法更改)使用 org.apache.commons.httpclient 进行 http 通信。它没有指定 procxy 服务器,但它使用默认的 HttpConnection。有什么方法可以从命令行告诉 apache http 客户端使用代理服务器吗?
采纳答案by dogbane
Unfortunately, I don't think you can. The only way is for the application to read the System property and then set it in the DefaultHttpParamsobject.
不幸的是,我认为你不能。唯一的方法是让应用程序读取 System 属性,然后在DefaultHttpParams对象中设置它。
Take a look at this threadon the httpclient-user group for more details.
有关更多详细信息,请查看httpclient-user 组上的此线程。
回答by userM1433372
When using the HTTPClient builder use the useSystemProperties() method to enable the standard JVM -D proxy parameters.
See http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html#useSystemProperties()
使用 HTTPClient 构建器时,请使用 useSystemProperties() 方法启用标准 JVM -D 代理参数。
见http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html#useSystemProperties()
Example:
例子:
CloseableHttpClient httpclient = HttpClients.custom()
.useSystemProperties()
.build();
Now use -Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800 to configure the proxy.
现在使用 -Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800 来配置代理。
回答by Bozho
I don't think so. But here is a code I found this code in an old project, which should've worked:
我不这么认为。但这是我在旧项目中发现的代码,它应该可以工作:
try {
String proxyHost = System.getProperty("https.proxyHost");
int proxyPort = 0;
try {
proxyPort = Integer.parseInt(System.getProperty("https.proxyPort"));
} catch (Exception ex) {
System.out.println("No proxy port found");
}
System.setProperty("java.net.useSystemProxies", "true");
ProxySelector ps = ProxySelector.getDefault();
List<Proxy> proxyList = ps.select(new URI(targetUrl));
Proxy proxy = proxyList.get(0);
if (proxy != null) {
InetSocketAddress addr = ((InetSocketAddress) proxy.address());
if (addr != null) {
proxyHost = addr.getHostName();
proxyPort = addr.getPort();
}
}
boolean useProxy = proxyHost != null && proxyHost.length() > 0;
if (useProxy) {
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
}
} catch (Exception ex) {
ex.printStackTrace();
}