@Override
  public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
      throws IOException, AuthFailureError {
    OkHttpClient client = okHttpClient.clone();
    int timeoutMs = request.getTimeoutMs();
    client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    com.squareup.okhttp.Request.Builder okHttpRequestBuilder =
        new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());

    Map<String, String> headers = request.getHeaders();

    for (final String name : headers.keySet()) {
      okHttpRequestBuilder.addHeader(name, headers.get(name));
    }

    for (final String name : additionalHeaders.keySet()) {
      okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
    }

    setConnectionParametersForRequest(okHttpRequestBuilder, request);

    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();

    StatusLine responseStatus =
        new BasicStatusLine(
            parseProtocol(okHttpResponse.protocol()),
            okHttpResponse.code(),
            okHttpResponse.message());

    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromOkHttpResponse(okHttpResponse));

    Headers responseHeaders = okHttpResponse.headers();

    for (int i = 0, len = responseHeaders.size(); i < len; i++) {
      final String name = responseHeaders.name(i), value = responseHeaders.value(i);

      if (name != null) {
        response.addHeader(new BasicHeader(name, value));
      }
    }

    return response;
  }
Beispiel #2
0
  public HttpResponse performMultiPartRequest(
      Request<?> request, Map<String, String> additionalHeaders)
      throws IOException, AuthFailureError {
    HttpUriRequest httpRequest = createMultiPartRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();
    // TODO: Reevaluate this connection timeout based on more wide-scale
    // data collection and possibly different for wifi vs. 3G.
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);

    /* Make a thread safe connection manager for the client */
    HttpClient httpClient = new DefaultHttpClient(httpParams);

    return httpClient.execute(httpRequest);
  }
Beispiel #3
0
  public HttpResponse performMultiPartRequest(
      Request<?> request, Map<String, String> additionalHeaders)
      throws IOException, AuthFailureError {
    HttpUriRequest httpRequest = createMultiPartRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();

    if (timeoutMs != -1) {
      HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
    }

    /* Make a thread safe connection manager for the client */
    HttpClient httpClient = new DefaultHttpClient(httpParams);

    return httpClient.execute(httpRequest);
  }
Beispiel #4
0
 @Override
 public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
     throws IOException, AuthFailureError {
   String url = request.getUrl();
   HashMap<String, String> map = new HashMap<String, String>();
   map.putAll(request.getHeaders());
   map.putAll(additionalHeaders);
   if (mUrlRewriter != null) {
     String rewritten = mUrlRewriter.rewriteUrl(url);
     if (rewritten == null) {
       throw new IOException("URL blocked by rewriter: " + url);
     }
     url = rewritten;
   }
   URL parsedUrl = new URL(url);
   HttpURLConnection connection = openConnection(parsedUrl, request);
   for (String headerName : map.keySet()) {
     connection.addRequestProperty(headerName, map.get(headerName));
   }
   setConnectionParametersForRequest(connection, request);
   // Initialize HttpResponse with data from the HttpURLConnection.
   ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
   int responseCode = connection.getResponseCode();
   if (responseCode == -1) {
     // -1 is returned by getResponseCode() if the response code could not be retrieved.
     // Signal to the caller that something was wrong with the connection.
     throw new IOException("Could not retrieve response code from HttpUrlConnection.");
   }
   StatusLine responseStatus =
       new BasicStatusLine(
           protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
   BasicHttpResponse response = new BasicHttpResponse(responseStatus);
   if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
     response.setEntity(entityFromConnection(connection));
   }
   for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
     if (header.getKey() != null) {
       Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
       response.addHeader(h);
     }
   }
   return response;
 }