我有一个神秘的问题!
我使用HttpClient()类来获取网页内容。
client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.USER_AGENT, useragent);
client.getParams().setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
client.getParams().setParameter("http.protocol.allow-circular-redirects", "true");
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
[...]
String html = getPage(client, new GetMethod(url));
和getPage()方法:
public String getPage(HttpClient myClient, GetMethod myMethod) {
BufferedReader br = null;
String retval = new String();
try {
int returnCode = myClient.executeMethod(myMethod);
if (returnCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + myMethod.getStatusLine());
} else {
System.out.println("buffer : ");
br = new BufferedReader(new InputStreamReader(myMethod.getResponseBodyAsStream()));
String readLine;
while (((readLine = br.readLine()) != null)) {
//System.err.println(readLine);
retval = retval.concat(readLine);
}
}
} catch (Exception e) {
System.out.println("Exception e : ");
System.err.println(e);
System.out.println("END Exception e : ");
} finally {
myMethod.releaseConnection();
if (br != null) {
try {
br.close();
} catch (Exception fe) {
System.out.println("Exception fe : ");
System.err.println(fe);
System.out.println("END Exception fe : ");
}
}
}
return retval;
}
这是shell结果:
Exception e :
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
END Exception e :
当我在自己的代码之后添加apache代码示例(https://hc.apache.org/httpclient-3.x/tutorial.html)时,我成功返回html页面:
public String getPage(HttpClient myClient, GetMethod myMethod) {
BufferedReader br = null;
String retval = new String();
try {
int returnCode = myClient.executeMethod(myMethod);
if (returnCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + myMethod.getStatusLine());
} else {
System.out.println("buffer : ");
br = new BufferedReader(new InputStreamReader(myMethod.getResponseBodyAsStream()));
String readLine;
while (((readLine = br.readLine()) != null)) {
//System.err.println(readLine);
retval = retval.concat(readLine);
}
}
} catch (Exception e) {
System.out.println("Exception e : ");
System.err.println(e);
System.out.println("END Exception e : ");
} finally {
myMethod.releaseConnection();
if (br != null) {
try {
br.close();
} catch (Exception fe) {
}
}
}
try {
// Execute the method.
int statusCode = myClient.executeMethod(myMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + myMethod.getStatusLine());
}
// Read the response body.
byte[] responseBody = myMethod.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println("here : " +new String(responseBody) +"\n enddd");
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
myMethod.releaseConnection();
}
return retval;
}
shell结果:
Exception e :
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
END Exception e :
juil. 19, 2013 6:53:26 PM org.apache.commons.httpclient.HttpMethodBase getResponseBody
WARNING: Going to buffer response body of large or unknown size. Using getResponseAsStream instead is recommended.
here : <!DOCTYPE html>
[...]
</html>
enddd
答案 0 :(得分:0)
这是你的错误来源:
client.getParams().setParameter("http.protocol.allow-circular-redirects", "true");
http.protocol.allow-circular-redirects
- 属性的documentation表示:
名称:
http.protocol.allow-circular-redirects
类型:布尔
因此它需要boolean
,但你给它一个字符串。因此,平台会尝试将string-object转换为boolean-object并失败(请参阅您的异常)。
使用setBooleanParameter(string, boolean)
-method或删除引号:
client.getParams().setParameter("http.protocol.allow-circular-redirects", true);
此外,仅供参考:抛出的异常,未返回。