HttpComponents组件特性探究
作者:网络转载 发布时间:[ 2012/11/26 10:37:29 ] 推荐标签:
getHttpClient().getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
}
/**
* Create a Commons HttpMethodBase object for the given HTTP method and URI
* specification.
*
* @param httpMethod the HTTP method
* @param uri the URI
* @return the Commons HttpMethodBase object
*/
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
switch (httpMethod) {
case GET:
return new HttpGet(uri);
case DELETE:
return new HttpDelete(uri);
case HEAD:
return new HttpHead(uri);
case OPTIONS:
return new HttpOptions(uri);
case POST:
return new HttpPost(uri);
case PUT:
return new HttpPut(uri);
case TRACE:
return new HttpTrace(uri);
default:
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
}
}
/**
* Execute the given method on the provided URI.
*
* @param method the HTTP method to execute (GET, POST, etc.)
* @param url the fully-expanded URL to connect to
* @param responseHandler httpClient will automatically take care of
* ensuring release of the connection back to the connection
* manager regardless whether the request execution succeeds or
* causes an exception,if using this response handler
* @return an response object's string representation
* @throws IOException
* @throws ClientProtocolException
*/
public String doExecuteRequest(HttpMethod httpMethod, URI uri,
ResponseHandler responseHandler)
throws ClientProtocolException, IOException {
return httpClient.execute(createHttpUriRequest(httpMethod, uri), responseHandler);
}
public InputStream doExecuteRequest(HttpMethod httpMethod, URI uri)
throws ClientProtocolException, IOException {
//1.
HttpUriRequest httpUriRequest = createHttpUriRequest(httpMethod, uri);
//2.
HttpResponse response = httpClient.execute(httpUriRequest);
//3.
validateResponse(response);
//4.
return getResponseBody(response);
}
/**
* Validate the given response, throwing an exception if it does not
* correspond to a successful HTTP response.
*
* Default implementation rejects any HTTP status code beyond 2xx, to avoid
* parsing the response body and trying to deserialize from a corrupted
* stream.
*
* @param config the HTTP invoker configuration that specifies the target
* service
* @param response the resulting HttpResponse to validate
* @throws NoHttpResponseException
* @throws java.io.IOException if validation failed
*/
protected void validateResponse(HttpResponse response) throws IOException {
StatusLine status = response.getStatusLine();
if (status.getStatusCode() >= 300) {
throw new NoHttpResponseException(
"Did not receive successful HTTP response: status code = "
+ status.getStatusCode() + ", status message = ["
+ status.getReasonPhrase() + "]");
}
}
/**
* Extract the response body
*
* The default implementation simply fetches the response body stream. If
* the response is recognized as GZIP response, the InputStream will get
* wrapped in a GZIPInputStream.
*
* @param httpResponse the resulting HttpResponse to read the response body
* from
* @return an InputStream for the response body
* @throws java.io.IOException if thrown by I/O methods
* @see #isGzipResponse
* @see java.util.zip.GZIPInputStream
*/
protected InputStream getResponseBody(HttpResponse httpResponse) throws IOException {
if (isGzipResponse(httpResponse)) {
return new GZIPInputStream(httpResponse.getEntity().getContent());
} else {
return httpResponse.getEntity().getContent();
}
}
/**
* Determine whether the given response indicates a GZIP response.
*
* The default implementation checks whether the HTTP "Content-Encoding"
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11