2012-10-26 52 views
0

得到錯誤,同時通過特定的將localAddress綁定的HttpClient 4.1請求本地地址

這種結合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了很多,但無法找到任何關於此。

這是一個操作系統相關的問題,在Win7上即時通訊?

任何幫助我們將不勝感激。

回答

0

Sry基因,但無法評論的問題,所以我會盡力回答: 99%的機會,問題192.168.43.98界面,檢查一下他是否工作良好

PS:您可以通過測試卷曲: 捲曲--interface 「192.168.43.98」 http://what-is-my-ip.net/?json

+0

$捲曲--interface 「192.168.43.98」 http://what-is-my-ip.net/?json 捲曲:(7)couldn」 t連接到主機 –

+0

當192.168.43.98接口關閉時顯示** curl:(45)綁定失敗,出現errno 125:無法分配請求的地址** –

+0

所以問題出在適配器或網絡接口192.168.43。 98 –