关于httpclient的问题

rjm37032408 rjm37032408 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我的程序是想输出每个网页地址的响应状态,但是有一种状态好像是网络异常,提示信息下面标红的字体,我想不让红色的部分打印的出来,谢谢各位了!

状态结果:4-HTTP/1.1 404 Not Found
状态结果:2-HTTP/1.1 404 Not Found
状态结果:1-HTTP/1.1 200 OK
3 - error: java.net.UnknownHostException: hc.apacwhe.org
2012-8-9 10:48:26 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: I/O exception (java.net.SocketException) caught when processing request: Connection reset
2012-8-9 10:48:27 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: Retrying request
2012-8-9 10:48:38 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: I/O exception (java.net.SocketException) caught when processing request: Connection reset
2012-8-9 10:48:38 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: Retrying request
2012-8-9 10:48:50 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: I/O exception (java.net.SocketException) caught when processing request: Connection reset
2012-8-9 10:48:50 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: Retrying request

5 - error: java.net.SocketException: Connection reset


============================================
下面是我的程序:

public class ClientMultiThreaded {

public static void main(String[] args) throws Exception {

HttpParams params = new BasicHttpParams();

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

//创建一个与ThreadSafeClientConnManager HttpClient类。
ClientConnectionManager cm = new ThreadSafeClientConnManager(schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm);

String[] urisToGet = {
"http://hc.apache.org/",
"http://hc.apache.org/httpcodmponents-core/",
"http://hc.apacwhe.org/httpcomponents-client/",
"http://svn.apache.org/visewvc/httpcomponents/",
"http://210.41.138.11:8080/opac/book/queryOut.jsp?kind=simple&type=number&word=7-80575-974-X"
};

// create a thread for each URI
GetThread[] threads = new GetThread[urisToGet.length];
for (int i = 0; i < threads.length; i++) {
HttpGet httpget = new HttpGet(urisToGet[i]);
threads[i] = new GetThread(httpClient, httpget, i + 1);

}

// start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
}

// join the threads
for (int j = 0; j < threads.length; j++) {
threads[j].join();
}

httpClient.getConnectionManager().shutdown();

}

/**
* A thread that performs a GET.
*/
static class GetThread extends Thread {

private final HttpClient httpClient;
private final BasicHttpContext context;
private final HttpGet httpget;
private final int id;

public GetThread(HttpClient httpClient, HttpGet httpget, int id) {
this.httpClient = httpClient;
this.context = new BasicHttpContext();
this.httpget = httpget;
this.id = id;
}

/**
* Executes the GetMethod and prints some status information.
*/
@Override
public void run() {

//System.out.println(id + " - about to get something from :" + httpget.getURI());

try {

HttpResponse response = httpClient.execute(httpget);
//HttpResponse statusCode = httpClient.execute(httpget);

//System.out.println(id + " - about:" + httpget.getURI() + response.getStatusLine());
//int statusCode = httpClient.execute(httpget);

System.out.println("状态结果:" + id + "-" + response.getStatusLine());

}catch (IOException e) {
httpget.abort();

System.out.println(id + " - error: " + e);
}finally{
//释放连接
httpget.releaseConnection();
}
}
}


...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
rjm37032408
Java SE 62,621 成员 307,251 内容