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

huangapple 未分类评论48阅读模式
英文:

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

  1. Caused by: feign.RetryableException: Malformed reply from SOCKS server executing GET

I identified the line of code causing this error which is

  1. OkHttpClient.Builder builder = new OkHttpClient.Builder();
  2. ...
  3. 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

  1. proxySelector.setDefault(null)

but that unfortunately didn't resolve my issue.

Thanks for your help!

答案1

得分: 1

创建两个OkHttpClient实例,一个配置了代理,另一个没有配置。

  1. builder.proxy(Proxy.NO_PROXY);

如果您使用OkHttpClient.newBuilder()从一个客户端创建另一个客户端,则它们将共享ExecutorService和其他资源。

英文:

Create two instances of OkHttpClient, one with the proxy configured and one with none.

  1. 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 客户端发送的请求,代理列表是空的:

  1. ProxySelector proxySelector = new ProxySelector() {
  2. @Override
  3. public List<Proxy> select(URI uri) {
  4. if (uri.toString().contains("HOST_NAME")) {
  5. return List.of();
  6. }
  7. return List.of(
  8. new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxyHost, socksProxyPort))
  9. );
  10. }
  11. @Override
  12. public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
  13. }
  14. };
  15. 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:

  1. ProxySelector proxySelector = new ProxySelector() {
  2. @Override
  3. public List&lt;Proxy&gt; select(URI uri) {
  4. if (uri.toString().contains(&quot;HOST_NAME&quot;)) {
  5. return List.of();
  6. }
  7. return List.of(
  8. new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxyHost, socksProxyPort)));
  9. }
  10. @Override
  11. public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
  12. }
  13. };
  14. builder.proxySelector(proxySelector);

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

发表评论

匿名网友

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

确定