0

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

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

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

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

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

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

我搜索了Unirestand OKHttp,但我找不到任何好的解决方案

4

1 回答 1

0

我刚刚通过创建自定义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);
    }
}

我试图绑定本地IP地址createSocket()然后我只是用MySocketFactoryOKHttp

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
SocketFactory socketFactory = 
MySocketFactory.getDefault("192.168.1.2");
httpClient.socketFactory(socketFactory);
于 2020-04-22T20:56:19.003 回答