日期:2014-05-17  浏览次数:20802 次

用HttpClient抓取网页返回值问题
public String downFile(String url){
String filePath=null;
//生成HttpClient对象并设置参数
HttpClient http=new HttpClient();
//设置HttpClient对象并设置连接超时5S
http.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
//生成GetMethod对象并设置参数
GetMethod method=new GetMethod(url);
//设置GET请求超时5S
method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,"5000");
//设置请求重试处理
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//执行GET请求
try{
System.out.println(http.executeMethod(method));
int statusCode=http.executeMethod(method);
//判断访问状态码
if(statusCode!=HttpStatus.SC_OK){
System.err.println("Method failes"+method.getStatusLine());
filePath=null;
}
//处理HTTP响应内容
byte[] responseBody=method.getResponseBody();
//根据网页URL生成保存文件名
filePath="d:\\temp"+getFileNameByUrl(url, method.getResponseHeader("Content-Type").getValue());
saveToLocal(responseBody, filePath);
}catch(Exception e){
System.out.println("请检查网址");
e.printStackTrace();
}finally{
method.releaseConnection();
}
return filePath;
}

就这个方法int statusCode=http.executeMethod(method);报错
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.apache.commons.httpclient.HttpMethodDirector.applyConnectionParams(HttpMethodDirector.java:353)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at com.qyty.DownLoadFile.downFile(DownLoadFile.java:59)
at com.qyty.MyCrawlet.crawling(MyCrawlet.java:34)
at com.qyty.MyCrawlet.main(MyCrawlet.java:46)
怎么报数值类型转换异常,难道要改源码JAR里面的编码?求各位大神帮忙解答下,谢谢
用的是HTTPCLIENT3的JAR包,4不好用。

------解决方案--------------------
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
}

------解决方案--------------------
String 转int 出错。http.executeMethod(method);