final HttpClient httpClient = new HttpClient();
// create GET instance
final GetMethod getMethod = new GetMethod(arResult.getRefUrl());
// system default recovery policy
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
// execute getMethod
final int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// read content
final byte[] responseBody = getMethod.getResponseBody();
// handle content
System.out.println(new String(responseBody));
} catch (final HttpException e) {
// Fatal Exception wrong protocol or content error
LOG.error("wrong protocol or content error", e);
} catch (final IOException e) {
// network exception
// e.printStackTrace();
LOG.error("network exception", e);
} finally {
// release connection
getMethod.releaseConnection();
}
java http httpclient
最新推荐文章于 2024-09-20 15:32:13 发布