将HttpClient 4.1请求绑定到本地地址

时间:2012-10-26 17:46:01

标签: java sockets httpclient

通过特定的localAddress

绑定HttpClient请求时出错

这是代码

    HttpParams httpsParams = new BasicHttpParams();
    ConnRouteParams.setLocalAddress(httpsParams, InetAddress.getByAddress(new byte[]{ (byte)192, (byte)168, (byte)43, (byte)98 }));

    DefaultHttpClient httpClient = new DefaultHttpClient(httpsParams);

    URL url = new URL("http://what-is-my-ip.net/?json");

    HttpGet method = new HttpGet(url.toURI());

    HttpResponse response = httpClient.execute(method);
    InputStream is = response.getEntity().getContent();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024];
    int nbRead;
    while ((nbRead = is.read(buffer)) != -1) {
        bos.write(buffer, 0, nbRead);
    }

    is.close();

    System.out.println(new String(bos.toByteArray()));

这是错误

Oct 26, 2012 10:48:57 PM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: Network is unreachable: connect
Oct 26, 2012 10:48:57 PM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: Retrying connect
Oct 26, 2012 10:48:57 PM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: Network is unreachable: connect
Oct 26, 2012 10:48:57 PM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: Retrying connect
Oct 26, 2012 10:48:57 PM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: Network is unreachable: connect
Oct 26, 2012 10:48:57 PM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: Retrying connect
Exception in thread "main" java.net.SocketException: Network is unreachable: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
    at utils.NewClass.main(NewClass.java:49)

我做错了吗? 请帮忙,我google了很多但却找不到任何相关内容。

这是与OS相关的问题吗,我在win7上?

任何帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

Sry,但无法评论问题,所以我会尽力回答: 99.1%的可能性在192.168.43.98接口中出现问题,检查它是否正常工作

PS:你可以通过curl测试它: curl --interface“192.168.43.98”http://what-is-my-ip.net/?json