【问题标题】:Can't connect to websocket through proxy无法通过代理连接到 websocket
【发布时间】:2020-05-31 11:57:55
【问题描述】:

我正在尝试使用 Java 的 okhttp3 库通过我自己的 http 代理连接到 websocket。

我已在 EC2 实例上设置代理并正确配置了安全组。我可以确认它有效:

time curl -s --proxy http://X.X.X.X:8888 https://api.binance.com/api/v3/ping

real    0m0.597s
user    0m0.040s
sys     0m0.008s

但是,我尝试通过代理连接到 websocket 失败了。下面是代码。

OkHttpClient client = new OkHttpClient.Builder()
        .proxy(new Proxy(Type.HTTP, new InetSocketAddress("X.X.X.X", 8888)))
        .build();

Request request = new Request.Builder()
        .url("wss://fstream.binance.com/stream")
        .build();

WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() {
    @Override
    public void onFailure(final WebSocket webSocket, final Throwable t, final Response response) {
        t.printStackTrace();
    }   
}); 

如果我删除.proxy(new Proxy(Type.HTTP, new InetSocketAddress("X.X.X.X", 8888))) 行,那么它工作得很好,但如果我有那行,我会得到异常:

java.io.IOException: Unexpected response code for CONNECT: 403
    at okhttp3.internal.connection.RealConnection.createTunnel(RealConnection.kt:447)
    at okhttp3.internal.connection.RealConnection.connectTunnel(RealConnection.kt:235)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:170)
    at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:236)
...
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
    at okhttp3.RealCall$AsyncCall.run(RealCall.kt:136)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

我不知道该怎么办?我可以通过终端访问代理,但无法通过它连接到 websocket。

【问题讨论】:

    标签: java http websocket proxy okhttp


    【解决方案1】:

    解决了。我的 tinyproxy 配置文件不允许我的 IP 地址。

    【讨论】:

      猜你喜欢
      • 2018-01-27
      • 2017-04-25
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      相关资源
      最近更新 更多