英文:
Malformed reply from SOCKS server when using OkHttpClient
问题
I'm sending REST requests in Java using a feign client which works perfectly fine, however when additionally using an OkHttpClient I get an error message
Caused by: feign.RetryableException: Malformed reply from SOCKS server executing GET
I identified the line of code causing this error which is
OkHttpClient.Builder builder = new OkHttpClient.Builder();
...
builder.proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxyHost, socksProxyPort)));
However since I both need the OkHttpClient and to send requests using the feign client I can't just remove this but instead have to find a workaround. Is there a way to reset the proxy settings for as long as I'm sending the requests via feign and set them back afterwards? I tried setting the default proxy server to null using
proxySelector.setDefault(null)
but that unfortunately didn't resolve my issue.
Thanks for your help!
英文:
I'm sending REST requests in Java using a feign client which works perfectly fine, however when additionally using an OkHttpClient I get an error message
Caused by: feign.RetryableException: Malformed reply from SOCKS server executing GET
I identified the line of code causing this error which is
OkHttpClient.Builder builder = new OkHttpClient.Builder();
...
builder.proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxyHost, socksProxyPort)));
However since I both need the OkHttpClient and to send requests using the feign client I can't just remove this but instead have to find a workaround. Is there a way to reset the proxy settings for as long as I'm sending the requests via feign and set them back afterwards? I tried setting the default proxy server to null using
proxySelector.setDefault(null)
but that unfortunately didn't resolve my issue.
Thanks for your help!
答案1
得分: 1
创建两个OkHttpClient实例,一个配置了代理,另一个没有配置。
builder.proxy(Proxy.NO_PROXY);
如果您使用OkHttpClient.newBuilder()
从一个客户端创建另一个客户端,则它们将共享ExecutorService
和其他资源。
英文:
Create two instances of OkHttpClient, one with the proxy configured and one with none.
builder.proxy(Proxy.NO_PROXY);
If you use OkHttpClient.newBuilder()
to create one client from the other they'll share an ExecutorService
and other resources.
答案2
得分: 0
我通过分配一个新的 ProxySelector 而不是实际的代理来解决了这个问题。在覆盖 select 方法时,我确保了对使用 Feign 客户端发送的请求,代理列表是空的:
ProxySelector proxySelector = new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
if (uri.toString().contains("HOST_NAME")) {
return List.of();
}
return List.of(
new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxyHost, socksProxyPort))
);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
};
builder.proxySelector(proxySelector);
英文:
I fixed the Problem by assigning a new ProxySelector instead of the actual proxy. When overriding the select method I made sure the proxy list is empty for requests sent using the feign client:
ProxySelector proxySelector = new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
if (uri.toString().contains("HOST_NAME")) {
return List.of();
}
return List.of(
new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxyHost, socksProxyPort)));
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
};
builder.proxySelector(proxySelector);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论