如何配置 apache httpcore 4 使用代理?
我正在尝试使用最新的(4.0.1)Apache http 核心组件库。 但是,我的网络浏览器通过代理 - 假设它是 myproxy.com:9191。 有人可以提供一些示例代码来获取简单的 http get 来使用它作为代理吗?
我尝试在代码开头添加以下内容,但没有任何乐趣:
ProxySelector.setDefault(new ProxySelector() {
public List<Proxy> select(URI uri) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("myproxy.com", 9191);
return Arrays.asList(new Proxy[]{proxy)});
}
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
ioe.printStackTrace();
}
});
I'm trying to use the latest (4.0.1) Apache http core components library. However, my web browser goes through a proxy - suppose it is myproxy.com:9191. Could someone provide some sample code for getting a simple http get to use this as a proxy?
I've tried adding the following at the beginning of my code, but had no joy:
ProxySelector.setDefault(new ProxySelector() {
public List<Proxy> select(URI uri) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("myproxy.com", 9191);
return Arrays.asList(new Proxy[]{proxy)});
}
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
ioe.printStackTrace();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在没有答案的情况下,这就是我发现的。
首先,对于这种事情,你不仅仅想使用http核心库,你想使用 httpclient 同样,请确保从下载页面下载两者。
其次,使用这段代码:
In the absence of an answer, here's what I found out.
Firstly, for this sort of thing, you don't just want to use the http core library, you want to use httpclient as well, make sure you download both from the download page.
Secondly, use this code: