“使用OkHttpClient时从SOCKS服务器收到的响应格式错误”

huangapple go评论95阅读模式
英文:

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&lt;Proxy&gt; select(URI uri) {
                if (uri.toString().contains(&quot;HOST_NAME&quot;)) {
                    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);

huangapple
  • 本文由 发表于 2020年5月19日 18:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61888814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定