关于httpclient的问题

rjm37032408 2012-08-09 11:17:47
我的程序是想输出每个网页地址的响应状态,但是有一种状态好像是网络异常,提示信息下面标红的字体,我想不让红色的部分打印的出来,谢谢各位了!

状态结果: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();
}
}
}


...全文
524 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
scbb 2012-08-10
  • 打赏
  • 举报
回复
程序最前面


System.setErr(new PrintStream(new FileOutputStream("anyfile.txt")));
rjm37032408 2012-08-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

Java code


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

System.out.println(id + " - error: " + e);
}

改为

catch (IOException e) {
httpget.abort();
}
[/Quote]

不行的,我都试过了,还是自动打印出红体字那部分,是不是跟配置什么的有关系啊!!
a395885670 2012-08-09
  • 打赏
  • 举报
回复
把这个不想显示的异常用个catch捕获 然后不打印出来
噢噢噢噢 2012-08-09
  • 打赏
  • 举报
回复


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

System.out.println(id + " - error: " + e);
}

改为

catch (IOException e) {
httpget.abort();
}
zhangjin109 2012-08-09
  • 打赏
  • 举报
回复
不懂这是什么

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧