public static void test(String host,int port) throws HttpException, IOException {
HttpClient client = new HttpClient();
// 设置代理服务器地址和端口
//client.getHostConfiguration().setProxy("221.214.208.17", 3128);
client.getHostConfiguration().setProxy(host, port);
// 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https
// 使用POST方法
HttpMethod method = new PostMethod("http://www.7y8.com/V/ip.asp");
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印返回的信息
//String result = method.getResponseBodyAsString();
StringBuffer rets = new StringBuffer();
InputStream responseBody = method.getResponseBodyAsStream();
InputStreamReader in = new InputStreamReader(responseBody, "gb2312");
BufferedReader br = new BufferedReader(in);
String s = "";
while ((s = br.readLine()) != null) {
rets.append(s + "\n");
}
s = "";
if (rets.toString().contains(host)) {
box = new MessageBox(shell, SWT.CENTER);
box.setText("IP测试!");
box.setMessage("代理IP可用");
box.open();
}else{
box = new MessageBox(shell, SWT.CENTER);
box.setText("IP测试!");
box.setMessage("代理IP不可用");
box.open();
}
br.close();
in.close();
writeToTXT("C:\\Documents and Settings\\Administrator\\桌面\\xxx.html",rets);
// 释放连接
method.releaseConnection();
}
2010-4-11 1:19:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
信息: I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect
2010-4-11 1:19:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
信息: Retrying request
上面程序可以简单判断一个代理IP是否可用,但是有一种代理IP无法响应会引发以下异常,并且程序主界面同时会无法响应,问下这种异常该如何处理,因为IP最后是传进来的,这段代码里的IP我只是测试用,所以如果遇到无法响应的情况就无法处理了,希望得到满意的答复,谢谢!