使Okhttp3绑定到特定的本地地址

时间:2019-06-17 19:32:24

标签: java okhttp3 apache-karaf

我想将通过okhttp3发起的HTTP客户端连接绑定到特定的网络接口。 VM上将配置许多vNIC,并且防火墙规则是特定的。因此,我想控制要使用哪个vNIC。我也计算出了正确的InetAddress。

也。我已经尝试过Global state and local address in OkHttp中建议的解决方案,但仍然会遇到ConnectionException。有人可以建议我使它无法正常工作吗?

      OkHttpClient okclient = new OkHttpClient.Builder()
              .retryOnConnectionFailure(false)
              .socketFactory(new CustomSocketFactory())
              .build();
      Request req = new Request.Builder()
              .url("http://www.google.com")
              .get()
              .build();
      Response rsp = okclient.newCall(req).execute();

下面是CustomSocketFactory,它与我上面提到的上述stackoverflow问题几乎相同。

class CustomSocketFactory extends SocketFactory
{
    private static final String ERROR_MSG = "Only unconnected sockets are supported";
    final SocketFactory defaultSocketFactory;
    final InetAddress localAddress;
    final int localPort = 0;

    public CustomSocketFactory() throws Exception
    {
        System.out.println("[creating a custom socket factory]");
        localAddress = InetAddress.getLocalHost();
        defaultSocketFactory = SocketFactory.getDefault();
    }

    @Override
    public Socket createSocket() throws IOException
    {
        System.out.println("I am here");
        Socket s = defaultSocketFactory.createSocket();
        s.bind(new InetSocketAddress(localAddress, localPort));
        return s;
    }

    @Override
    public Socket createSocket(final String remoteAddress, final int remotePort)
    {
        throw new UnsupportedOperationException(ERROR_MSG);
    }

    @Override
    public Socket createSocket(final InetAddress remoteAddress, final int remotePort)
    {
        throw new UnsupportedOperationException(ERROR_MSG);
    }

    @Override
    public Socket createSocket(final String remoteAddress,
                               final int remotePort,
                               final InetAddress localAddress,
                               final int localPort)
    {
        throw new UnsupportedOperationException(ERROR_MSG);
    }

    @Override
    public Socket createSocket(final InetAddress remoteAddress,
                               final int remotePort,
                               final InetAddress localAddress,
                               final int localPort)
    {
        throw new UnsupportedOperationException(ERROR_MSG);
    }

}


Error received: 

java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:244)
    at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:277)
    at com.okhttp3.CustomSocketFactory.createSocket(TestOkClient.java:74)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:257)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:183)
    at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224)
    at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108)
    at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.java:88)
    at okhttp3.internal.connection.Transmitter.newExchange(Transmitter.java:169)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:221)
    at okhttp3.RealCall.execute(RealCall.java:81)
    at com.okhttp3.TestOkClient.main(TestOkClient.java:40)

1 个答案:

答案 0 :(得分:0)

通过使用环回设备InetAddress.getLocalHost(),为什么看起来最正确?

这是一个类似的解决方案https://github.com/yschimke/okurl/blob/master/src/main/kotlin/com/baulsupp/okurl/network/InterfaceSocketFactory.kt