どのように指定すればよいですか地元のアドレスは、java.います。URLConnection?

StackOverflow https://stackoverflow.com/questions/91678

  •  01-07-2019
  •  | 
  •  

質問

私のTomcatインスタンスは複数のIPアドレスがたいコントロールのソースIPアドレスを利用開く時に URLConnection.

できるかを指定します。

役に立ちましたか?

解決

こん:

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

まで行います。ぜ扱いの例外でもoffでコース変更の値に合わせてシナリオ。

あとは省略し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はdown(192.168.1.11しています。今度の玄関口なかったってくるのではないかと思いまfail?

は、あなたの携帯ちょっとセットのプロキシURL。openConnection.プロキシ可能で、ローカルホストで一時的に保存してから非常に簡単なプロキシと結合する現地の住所をクライアントのソケットが開かれます。

できない場合は変更のソースのURLが接続されて置き換えることができURLStreamHandlerからかける場合は、URLコンストラクタは世界的によります。setURLStreamHandlerFactory.のURLStreamHandlerを委譲するデフォルトのhttp/httpsのハンドラ、改変、openConnectionます。

より極限法の考え方に完全に置き換えにハンドラ(その実施およびJRE).また、代替(オープンソースのhttpトが可能です。

設定を手動でソケット作品。--------

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;
}    
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top