繁体   English   中英

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'));

我搜索了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()中绑定本地 ip 地址然后我只是在MySocketFactory中使用了OKHttp

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM