gpt4 book ai didi

java - 让 Okhttp3 绑定(bind)到特定的本地地址

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:50 24 4
gpt4 key购买 nike

我想将来自 okhttp3 的 HTTP 客户端连接绑定(bind)到特定的网络接口(interface)。虚拟机上会配置许多 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)

最佳答案

看起来基本正确,为什么要使用环回设备InetAddress.getLocalHost()

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

关于java - 让 Okhttp3 绑定(bind)到特定的本地地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56637792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com