@SuppressWarnings("unchecked")
  public ClientResponse execute(ClientRequest request) throws Exception {
    String uri = request.getUri();
    final HttpRequestBase httpMethod = createHttpMethod(uri, request.getHttpMethod());
    try {
      loadHttpMethod(request, httpMethod);

      final HttpResponse res = httpClient.execute(httpMethod, httpContext);

      final BaseClientResponse response = new BaseClientResponse(null, this);
      BaseClientResponseStreamFactory sf =
          new BaseClientResponseStreamFactory() {
            InputStream stream;

            public InputStream getInputStream() throws IOException {
              if (stream == null) {
                HttpEntity entity = res.getEntity();
                if (entity == null) return null;
                stream = new SelfExpandingBufferredInputStream(entity.getContent());
              }
              return stream;
            }

            public void performReleaseConnection() {
              // Apache Client 4 is stupid, You have to get the InputStream and close it if there is
              // an entity
              // otherwise the connection is never released. There is, of course, no close() method
              // on response
              // to make this easier.
              try {
                if (stream != null) {
                  stream.close();
                } else {
                  InputStream is = getInputStream();
                  if (is != null) {
                    is.close();
                  }
                }
              } catch (Exception ignore) {
              }
            }
          };
      response.setStreamFactory(sf);
      response.setAttributes(request.getAttributes());
      response.setStatus(res.getStatusLine().getStatusCode());
      response.setHeaders(extractHeaders(res));
      response.setProviderFactory(request.getProviderFactory());
      return response;
    } finally {
      cleanUpAfterExecute(httpMethod);
    }
  }
Example #2
0
  private HttpResponse getHttpResponse(HttpRequestBase httpRequest, HttpContext localContext)
      throws WeiboException {
    HttpResponse response = null;
    try {
      if (localContext != null) {
        response = httpClient.execute(httpRequest, localContext);
      } else {
        response = httpClient.execute(httpRequest);
      }
    } catch (ConnectTimeoutException e) {

      AppLogger.e(e.getMessage());
      throw new WeiboException(GlobalContext.getInstance().getString(R.string.timeout), e);

    } catch (ClientProtocolException e) {
      AppLogger.e(e.getMessage());
      throw new WeiboException(GlobalContext.getInstance().getString(R.string.timeout), e);

    } catch (IOException e) {
      AppLogger.e(e.getMessage());
      throw new WeiboException(GlobalContext.getInstance().getString(R.string.timeout), e);
    }
    return response;
  }
Example #3
0
 protected SilkHttpResponse performRequest(final HttpUriRequest request) throws SilkHttpException {
   if (mHeaders.size() > 0) {
     for (SilkHttpHeader header : mHeaders) request.setHeader(header.getName(), header.getValue());
   }
   HttpResponse response;
   try {
     response = mClient.execute(request);
   } catch (Exception e) {
     reset();
     throw new SilkHttpException(e);
   }
   int status = response.getStatusLine().getStatusCode();
   if (status != 200) {
     reset();
     throw new SilkHttpException(response);
   }
   reset();
   return new SilkHttpResponse(response);
 }
Example #4
0
  /** don't need error message to show */
  private String doGetSaveFile(
      String url, String path, FileDownloaderHttpHelper.DownloadListener downloadListener) {

    URIBuilder uriBuilder;
    HttpGet httpGet = new HttpGet();
    try {
      uriBuilder = new URIBuilder(url);

      httpGet.setURI(uriBuilder.build());

      AppLogger.d(uriBuilder.build().toString());

    } catch (URISyntaxException e) {
      AppLogger.d(e.getMessage());
    }

    HttpResponse response = null;
    try {

      response = httpClient.execute(httpGet);

    } catch (ConnectTimeoutException ignored) {

      AppLogger.e(ignored.getMessage());

    } catch (ClientProtocolException ignored) {
      AppLogger.e(ignored.getMessage());

    } catch (IOException ignored) {
      AppLogger.e(ignored.getMessage());
    }

    if (response != null) {

      return FileDownloaderHttpHelper.saveFile(response, path, downloadListener);

    } else {
      return "";
    }
  }