我的程序是想输出每个网页地址的响应状态,但是有一种状态好像是网络异常,提示信息下面标红的字体,我想不让红色的部分打印的出来,谢谢各位了!状态结果: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();
}
}
}
状态结果: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();
}
}
}
httpget.abort();
System.out.println(id + " - error: " + e);
}改为catch (IOException e) {
httpget.abort();
}
System.setErr(new PrintStream(new FileOutputStream("anyfile.txt")));