代码之家  ›  专栏  ›  技术社区  ›  stian

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

  •  11
  • stian  · 技术社区  · 18 年前

    我的Tomcat实例正在侦听多个IP地址,但我想控制在打开 URLConnection .

    我如何指定?

    4 回复  |  直到 18 年前
        1
  •  6
  •   Javaxpert    18 年前

    这应该可以做到:

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

    你就完了。 别忘了很好地处理异常,当然也要更改值以适应您的场景。

    啊,我省略了进口声明

        2
  •  3
  •   stian    18 年前

    使用ApacheCommons 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)。下一个网关会被尝试还是会失败?

        3
  •  1
  •   Tom Hawtin - tackline    18 年前

    显而易见的可移植方法是在url.openconnection中设置代理。代理可以在本地主机中,然后可以编写一个非常简单的代理来绑定客户端套接字的本地地址。

    如果无法修改URL连接的源,则可以在调用URL构造函数时或通过url.seturlstreamHandlerFactory全局替换urlstreamHandler。然后,UrlstreamHandler可以委托给默认的HTTP/HTTPS处理程序,修改OpenConnection调用。

    更极端的方法是完全替换处理程序(可能在JRE中扩展实现)。或者,也可以使用其他(开源)HTTP客户端。

        4
  •  1
  •   Enzo    13 年前

    手动设置插座工作正常…

    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;
    }