我对获取请求有疑问。
我正在使用具有正确配置的代理设置的 Eclipse Juno(我可以检查更新并安装插件)我的操作系统是 Windows 7。
我已下载并安装
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
错误堆栈跟踪
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)