簡體   English   中英

如何在java.net.URLConnection上指定本地地址?

[英]How can I specify the local address on a java.net.URLConnection?

我的Tomcat實例正在偵聽多個IP地址,但我想控制打開URLConnection時使用的源IP地址。

我怎么指定這個?

這應該做的伎倆:

URL url = new URL(yourUrlHere);
Proxy proxy = new Proxy(Proxy.Type.DIRECT, 
    new InetSocketAddress( 
        InetAddress.getByAddress(
            new byte[]{your, ip, interface, here}), yourTcpPortHere));
URLConnection conn = url.openConnection(proxy);

你完成了。 不要忘記很好地處理異常,並且當然會更改值以適合您的場景。

啊,我省略了import語句

使用Apache commons HttpClient我也發現了以下工作(為了清楚起見,刪除了try / catch):

HostConfiguration hostConfiguration = new HostConfiguration();
byte b[] = new byte[4];
b[0] = new Integer(192).byteValue();
b[1] = new Integer(168).byteValue();
b[2] = new Integer(1).byteValue();
b[3] = new Integer(11).byteValue();

hostConfiguration.setLocalAddress(InetAddress.getByAddress(b));

HttpClient client = new HttpClient();
client.setHostConfiguration(hostConfiguration);
GetMethod method = new GetMethod("http://remoteserver/");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler(3, false));
int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {
    System.err.println("Method failed: " + method.getStatusLine());
}

byte[] responseBody = method.getResponseBody();
System.out.println(new String(responseBody));");

但是,我仍然想知道如果IP的網關關閉會發生什么(在這種情況下為192.168.1.11)。 下一個網關會被嘗試還是會失敗?

設置手動套接字工作正常...

private HttpsURLConnection openConnection(URL src, URL dest, SSLContext sslContext)
        throws IOException, ProtocolException {
    HttpsURLConnection connection = (HttpsURLConnection) dest.openConnection();
    HttpsHostNameVerifier httpsHostNameVerifier = new HttpsHostNameVerifier();
    connection.setHostnameVerifier(httpsHostNameVerifier);
    connection.setConnectTimeout(CONNECT_TIMEOUT);
    connection.setReadTimeout(READ_TIMEOUT);
    connection.setRequestMethod(POST_METHOD);
    connection.setRequestProperty(CONTENT_TYPE, SoapConstants.CONTENT_TYPE_HEADER);
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setSSLSocketFactory(sslContext.getSocketFactory());
    if ( src!=null ) {
        InetAddress inetAddress = InetAddress.getByName(src.getHost());
        int destPort = dest.getPort();
        if ( destPort <=0 ) 
            destPort=SERVER_HTTPS_PORT;
        int srcPort = src.getPort();
        if ( srcPort <=0 ) 
            srcPort=CLIENT_HTTPS_PORT;
         connectionSocket = connection.getSSLSocketFactory().createSocket(dest.getHost(), destPort, inetAddress, srcPort);
    }
    connection.connect();
    return connection;
}    

明顯的可移植方式是在URL.openConnection中設置代理。 代理可以在本地主機中,然后您可以編寫一個非常簡單的代理來綁定客戶端套接字的本地地址。

如果無法修改URL所連接的源,則可以在調用URL構造函數時替換URLStreamHandler,也可以通過URL.setURLStreamHandlerFactory全局替換URLStreamHandler。 然后,URLStreamHandler可以委托默認的http / https處理程序,修改openConnection調用。

更極端的方法是完全替換處理程序(可能在JRE中擴展實現)。 或者,可以使用備用(開源)http客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM