通过特定的 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)
难道我做错了什么 ?请帮忙,我用谷歌搜索了很多,但在这方面找不到任何东西。
这是与操作系统相关的问题吗,我在 win7 上?
任何帮助将不胜感激。