HttpComponents组件特性探究(4)

发表于:2012-11-23来源:Csdn作者:fengjia10点击数: 标签:开源框架
*/ public void setReadTimeout(int timeout) { Validate.isTrue(timeout = 0, Timeout must be a non-negative value); getHttpClient().getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);

  */

  public void setReadTimeout(int timeout) {

  Validate.isTrue(timeout >= 0, "Timeout must be a non-negative value");

  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();

  }

原文转自:http://www.ltesting.net