我遇到了get请求的问题。
我正在使用Eclipse Juno和正确配置的代理设置(我可以检查更新并安装插件)我的操作系统是Windows 7。
我已经下载并安装了HttpClient 4.2.5
(通过将所有jar复制到项目目录中并通过Eclipse添加所有Build Path)
3.Java class
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)
答案 0 :(得分:0)
Eclipse中的代理首选项本身与您的程序无关。您需要更改程序以使用必要的代理。
Apache HttpClient文档中的这一部分可能会有所帮助:HttpClient proxy configuration
答案 1 :(得分:0)
此处HttpClient
没有任何问题。可能的原因是连接故障。它是重新连接 - 检查防火墙问题,如果你提供更多信息,我可以帮助你更多。