gpt4 book ai didi

CURLOPT_INTERFACE 的 JAVA 替代方案

转载 作者:行者123 更新时间:2023-12-01 17:17:09 26 4
gpt4 key购买 nike

我的服务器上运行一个 Java 应用程序,它将请求发送到外部服务器。

我的服务器有 5 个不同的 IP 地址。

如何使用一个不同的 IP 地址发出每个请求?

例如,我想使用 192.168.1.2 发送第一个请求,并使用 192.168.1.3 发送第二个请求,依此类推。

在 PHP 中,我通过这段代码达到了这个目的

curl_setopt_array($curl , array(CURLOPT_INTERFACE => 'IpAddress'));

我搜索了UnirestOKHttp,但我找不到任何关于此的好的解决方案

最佳答案

我刚刚通过创建自定义SocketFactory解决了这个问题

    public abstract class MySocketFactory
{
private static javax.net.SocketFactory theFactory;

protected MySocketFactory() { /* NOTHING */ }


/**
* Returns a copy of the environment's default socket factory.
*
* @return the default <code>SocketFactory</code>
*/
public static javax.net.SocketFactory getDefault(String localIP) throws UnknownHostException {
synchronized (javax.net.SocketFactory.class) {
if (theFactory == null) {
theFactory = new DefaultSocketFactory(InetAddress.getByName(localIP));
}
else app.l("theFactory is not null");
}

return theFactory;
}


public static void setDefault(javax.net.SocketFactory factory) {
synchronized (javax.net.SocketFactory.class) {
theFactory = factory;
}
}


public Socket createSocket() throws IOException {

UnsupportedOperationException uop = new
UnsupportedOperationException();
SocketException se = new SocketException(
"Unconnected sockets not implemented");
se.initCause(uop);
throw se;
}
public abstract Socket createSocket(String host, int port)
throws IOException, UnknownHostException;


public abstract Socket
createSocket(String host, int port, InetAddress localHost, int localPort)
throws IOException, UnknownHostException;


public abstract Socket createSocket(InetAddress host, int port)
throws IOException;


public abstract Socket
createSocket(InetAddress address, int port,
InetAddress localAddress, int localPort)
throws IOException;
}


class DefaultSocketFactory extends javax.net.SocketFactory {
InetAddress localIP;
public DefaultSocketFactory(InetAddress localIP) {
this.localIP = localIP;
}
public Socket createSocket() throws IOException {
ServerSocket s = new ServerSocket(0);
//get an empty port
System.out.println("local port: " + s.getLocalPort());
//close the listener to re open the port
s.close();
int finalPort = s.getLocalPort() ;
SocketChannel channel = SocketChannel.open();
channel.socket().bind(new InetSocketAddress(localIP, finalPort));
return channel.socket();

}

public Socket createSocket(String host, int port)
throws IOException {
return new Socket(host, port);
}

public Socket createSocket(InetAddress address, int port)
throws IOException
{
return new Socket(address, port);
}

public Socket createSocket(String host, int port,
InetAddress clientAddress, int clientPort)
throws IOException
{
return new Socket(host, port, localIP, clientPort);
}

public Socket createSocket(InetAddress address, int port,
InetAddress clientAddress, int clientPort)
throws IOException
{
return new Socket(address, port, localIP, clientPort);
}
}

我尝试在createSocket()中绑定(bind)本地IP地址,然后我只是在OKHttp中使用MySocketFactory

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
SocketFactory socketFactory =
MySocketFactory.getDefault("192.168.1.2");
httpClient.socketFactory(socketFactory);

关于CURLOPT_INTERFACE 的 JAVA 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61362667/

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