Beispiel #1
0
  private String doGet(String url, Map<String, String> param) throws WeiboException {

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

      Set<String> keys = param.keySet();

      for (String key : keys) {
        String value = param.get(key);
        if (!TextUtils.isEmpty(value)) uriBuilder.addParameter(key, param.get(key));
      }

      httpGet.setURI(uriBuilder.build());

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

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

    CookieStore cookieStore = new BasicCookieStore();

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpResponse response = getHttpResponse(httpGet, localContext);

    if (response != null) {
      return dealWithResponse(response);
    } else {
      return "";
    }
  }
Beispiel #2
0
 protected HttpContext createHttpContext() {
   final HttpContext context = new BasicHttpContext();
   context.setAttribute(ClientContext.SCHEME_REGISTRY, getConnectionManager().getSchemeRegistry());
   context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes());
   context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs());
   context.setAttribute(ClientContext.COOKIE_STORE, getCookieStore());
   context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider());
   return context;
 }
Beispiel #3
0
  @Override
  protected final CloseableHttpResponse doExecute(
      final HttpHost target, final HttpRequest request, final HttpContext context)
      throws IOException, ClientProtocolException {

    Args.notNull(request, "HTTP request");
    // a null target may be acceptable, this depends on the route planner
    // a null context is acceptable, default context created below

    HttpContext execContext = null;
    RequestDirector director = null;
    HttpRoutePlanner routePlanner = null;
    ConnectionBackoffStrategy connectionBackoffStrategy = null;
    BackoffManager backoffManager = null;

    // Initialize the request execution context making copies of
    // all shared objects that are potentially threading unsafe.
    synchronized (this) {
      final HttpContext defaultContext = createHttpContext();
      if (context == null) {
        execContext = defaultContext;
      } else {
        execContext = new DefaultedHttpContext(context, defaultContext);
      }
      final HttpParams params = determineParams(request);
      final RequestConfig config = HttpClientParamConfig.getRequestConfig(params);
      execContext.setAttribute(ClientContext.REQUEST_CONFIG, config);

      // Create a director for this request
      director =
          createClientRequestDirector(
              getRequestExecutor(),
              getConnectionManager(),
              getConnectionReuseStrategy(),
              getConnectionKeepAliveStrategy(),
              getRoutePlanner(),
              getProtocolProcessor(),
              getHttpRequestRetryHandler(),
              getRedirectStrategy(),
              getTargetAuthenticationStrategy(),
              getProxyAuthenticationStrategy(),
              getUserTokenHandler(),
              params);
      routePlanner = getRoutePlanner();
      connectionBackoffStrategy = getConnectionBackoffStrategy();
      backoffManager = getBackoffManager();
    }

    try {
      if (connectionBackoffStrategy != null && backoffManager != null) {
        final HttpHost targetForRoute =
            (target != null)
                ? target
                : (HttpHost) determineParams(request).getParameter(ClientPNames.DEFAULT_HOST);
        final HttpRoute route = routePlanner.determineRoute(targetForRoute, request, execContext);

        final CloseableHttpResponse out;
        try {
          out = CloseableHttpResponseProxy.newProxy(director.execute(target, request, execContext));
        } catch (final RuntimeException re) {
          if (connectionBackoffStrategy.shouldBackoff(re)) {
            backoffManager.backOff(route);
          }
          throw re;
        } catch (final Exception e) {
          if (connectionBackoffStrategy.shouldBackoff(e)) {
            backoffManager.backOff(route);
          }
          if (e instanceof HttpException) {
            throw (HttpException) e;
          }
          if (e instanceof IOException) {
            throw (IOException) e;
          }
          throw new UndeclaredThrowableException(e);
        }
        if (connectionBackoffStrategy.shouldBackoff(out)) {
          backoffManager.backOff(route);
        } else {
          backoffManager.probe(route);
        }
        return out;
      } else {
        return CloseableHttpResponseProxy.newProxy(director.execute(target, request, execContext));
      }
    } catch (final HttpException httpException) {
      throw new ClientProtocolException(httpException);
    }
  }