套接字绑定到 URL.openconnection()。

huangapple go评论88阅读模式
英文:

Socket Binding at URL.openconnection()

问题

我该如何选择要发出请求的IP地址?服务器有多个IP地址绑定到它,有多个接口。我想要为这个请求使用特定的IP地址。

我使用了HttpsUrlConnection。

URL url = new URL("x");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
...
connection.setSSLSocketFactory(SSLSocketFactory);
connection.connect();

我在SSLSocketFactory中重写了所有的createSocket,将源IP绑定到我选择的IP上。问题是当我执行以下操作时:

URL url = new URL("x");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
...
connection.setSSLSocketFactory(SSLSocketFactory);
connection.connect();

我发现套接字被创建,并且我的sslsocketfactory使用了createSocket创建的套接字。我可以将其绑定到其他IP地址,但我想在openConnection时覆盖源IP地址。我应该在哪里进行覆盖?

英文:

JRE 8\

How do I select the IP address FROM which this request will be fired? Server has multiple IP addresses bind to it, multiple interfaces. I want to use a specific one for this request.

I have httpsurlconnection.

URL url = new Url("x");
HttpsUrlConnection connection = (HttpsUrlConnection) url.openConnection();
...
connection.setSSLSocketFactory(SSLSocketFactory);
connection.connect();

I overrided in SSLSocketFactory all createSocket with binding source ip to choosen by me. Problem is when I:

URL url = new Url("x");
HttpsUrlConnection connection = (HttpsUrlConnection) url.openConnection();
...
connection.setSSLSocketFactory(SSLSocketFactory);
connection.connect();

I see that socket is created and my sslsocketfactory using createSocket with created socket. I can bind then to other ip address but I want to override source IP address when openConnection. Where can I override it?

答案1

得分: 1

HttpsUrlConnection
使用

SSLSocketFactory.createSocket();
用以下方式覆盖:

Factory extends SSLSocketFactory{
    private SSLSocketFactory basicFactory;

    Factory(SSLSocketFactory basicFactory){
        this.basicFactory = basicFactory;
    }
...
    @Override
    public Socket createSocket() throws IOException {
        Socket socket = basicFactory.createSocket();
        socket.bind(sourceIpAddress);
        return socket;
    }
}
不要使用super.createSocket(),因为它会抛出SocketException("Unconnected sockets not implemented"),这将在HttpsClient中捕获,并使用NetworkClient创建带有随机源IP地址的java.net.Socket。
英文:
HttpsUrlConnection

using

SSLSocketFactory.createSocket();

Override this with something like

Factory extends SSLSocketFactory{
    private SSLSocketFactory basicFactory;

    Factory(SSLSocketFactory basicFactory){
        this.basicFactory = basicFactory;
    }
...
    @Override
    public Socket createSocket() throws IOException {
        Socket socket = basicFactory.createSocket();
        socket.bind(sourceIpAddress);
        return socket;
    }
}

Don't use super.createSocket() because it will throw SocketException("Unconnected sockets not implemented") which will be caught in HttpsClient and use NetworkClient to create socket with java.net.Socket with random source ip address

huangapple
  • 本文由 发表于 2020年8月5日 20:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63265582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定