JAVA CURLOPT_INTERFACE 的替代方案
JAVA alternative for CURLOPT_INTERFACE
我的服务器上有一个 Java 应用程序 运行,它向外部服务器发送请求。
我的服务器有 5 个不同的 IP 地址。
如何使用一个不同的 IP 地址发出每个请求?
例如,我想用 192.168.1.2 发送第一个请求,用 192.168.1.3 发送第二个请求,依此类推。
在 PHP 我用这段代码达到了这个目的
curl_setopt_array($curl , array(CURLOPT_INTERFACE => 'IpAddress'));
我搜索了 Unirest
和 OKHttp
,但找不到关于此的任何好的解决方案
我刚刚通过创建自定义 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()
中绑定本地IP地址,然后我只是在OKHttp
中使用了MySocketFactory
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
SocketFactory socketFactory =
MySocketFactory.getDefault("192.168.1.2");
httpClient.socketFactory(socketFactory);
我的服务器上有一个 Java 应用程序 运行,它向外部服务器发送请求。
我的服务器有 5 个不同的 IP 地址。
如何使用一个不同的 IP 地址发出每个请求?
例如,我想用 192.168.1.2 发送第一个请求,用 192.168.1.3 发送第二个请求,依此类推。
在 PHP 我用这段代码达到了这个目的
curl_setopt_array($curl , array(CURLOPT_INTERFACE => 'IpAddress'));
我搜索了 Unirest
和 OKHttp
,但找不到关于此的任何好的解决方案
我刚刚通过创建自定义 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()
中绑定本地IP地址,然后我只是在OKHttp
MySocketFactory
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
SocketFactory socketFactory =
MySocketFactory.getDefault("192.168.1.2");
httpClient.socketFactory(socketFactory);