/**
   * Creates and initializes an HttpResponse object suitable to be passed to an HTTP response
   * handler object.
   *
   * @param method The HTTP method that was invoked to get the response.
   * @param request The HTTP request associated with the response.
   * @return The new, initialized HttpResponse object ready to be passed to an HTTP response handler
   *     object.
   * @throws IOException If there were any problems getting any response information from the
   *     HttpClient method object.
   */
  private HttpResponse createResponse(
      HttpRequestBase method, Request<?> request, org.apache.http.HttpResponse apacheHttpResponse)
      throws IOException {
    HttpResponse httpResponse = new HttpResponse(request, method);

    if (apacheHttpResponse.getEntity() != null) {
      httpResponse.setContent(apacheHttpResponse.getEntity().getContent());
    }

    httpResponse.setStatusCode(apacheHttpResponse.getStatusLine().getStatusCode());
    httpResponse.setStatusText(apacheHttpResponse.getStatusLine().getReasonPhrase());
    for (Header header : apacheHttpResponse.getAllHeaders()) {
      httpResponse.addHeader(header.getName(), header.getValue());
    }

    return httpResponse;
  }
예제 #2
0
 private HttpResponse getHttpResult(HttpMethod method, int retry)
     throws ConnectException, HttpHelperException {
   HttpResponse httpResponse = new HttpResponse();
   this.initMethod(method, retry);
   InputStream is = null;
   BufferedReader reader = null;
   HttpClient client = createHttpClient();
   try {
     client.executeMethod(method);
   } catch (HttpException e1) {
     throw new ConnectException(e1);
   } catch (IOException e1) {
     throw new HttpHelperException(e1);
   }
   try {
     is = method.getResponseBodyAsStream();
     reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
     StringBuilder sb = new StringBuilder();
     char[] ch = new char[3096];
     while (reader.read(ch) != -1) {
       sb.append(ch);
     }
     httpResponse.setStatusCode(method.getStatusCode());
     httpResponse.setStatusText(method.getStatusText());
     httpResponse.setResponseText(sb.toString());
     return httpResponse;
   } catch (Exception e) {
     throw new HttpHelperException(e);
   } finally {
     method.releaseConnection();
     try {
       if (reader != null) {
         reader.close();
       }
     } catch (IOException e) {
       throw new HttpHelperException(e);
     }
     try {
       if (is != null) {
         is.close();
       }
     } catch (IOException e) {
       throw new HttpHelperException(e);
     }
   }
 }