publicstaticStringgetDoGetURL2(Stringurl,Stringcharset)throwsException{HttpClienthttpClient=newHttpClient();httpClient.getHttpConnectionManager().getParams().setConnectio...
public static String getDoGetURL2(String url, String charset)
throws Exception {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
5000);
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
String response = "";
try {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
Header[] headers = getMethod.getResponseHeaders();
for (Header h : headers)
System.out
.println(h.getName() + "------------ " + h.getValue());
byte[] responseBody = getMethod.getResponseBody(); // 读取为字节数组
response = new String(responseBody, charset);
System.out.println("----------response:" + response);
} catch (HttpException e) {
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
return response;
}
每一行代码的意思,谢谢!
展开