0

我对获取请求有疑问。

  1. 我正在使用具有正确配置的代理设置的 Eclipse Juno(我可以检查更新并安装插件)我的操作系统是 Windows 7。

  2. 我已下载并安装HttpClient 4.2.5(通过将所有 jar 复制到项目目录并通过 Eclipse 添加所有构建路径)

3.Java类

public class ConnectClass {

public static final String USER_AGENT = "Mozilla/5.0";

private String cookies;
private HttpClient client;

ConnectClass() {
    client = new DefaultHttpClient();
}

public static void main(String[] args) {
    String vogel = "http://www.vogella.com";

    ConnectClass connect = new ConnectClass();
    try {
        connect.doGet(vogel);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void doGet(String url) throws Exception {

    HttpGet request = new HttpGet(url);
    request.addHeader("User-Agent", USER_AGENT);
    HttpResponse response = client.execute(request);

    System.out.println("Response Code:"
            + response.getStatusLine().getStatusCode());

    BufferedReader rd = new BufferedReader(new InputStreamReader(response
            .getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

}

}

4 我有一个错误

INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: Network is unreachable: connect
июл 03, 2013 1:53:41 PM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: Retrying connect
  1. 错误堆栈跟踪

    java.net.SocketException: Network is unreachable: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:643)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
    at ConnectClass.doGet(ConnectClass.java:49)
    at ConnectClass.main(ConnectClass.java:37)
    
4

2 回答 2

0

Eclipse 本身的代理首选项与您的程序无关。您需要更改程序以使用必要的代理。

Apache HttpClient 文档中的这一部分可能会有所帮助: HttpClient 代理配置

于 2013-07-03T11:42:06.533 回答
0

这边没有问题HttpClient。可能的原因是连接中断。它说它正在重试连接 - 检查防火墙问题,如果您提供更多信息,我可以为您提供更多帮助。

于 2013-07-03T11:42:10.027 回答