Search in sources :

Example 31 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project openkit-android by OpenKit.

the class AsyncHttpClient method get.

/**
     * Perform a HTTP GET request and track the Android Context which initiated
     * the request with customized headers
     * 
     * @param url the URL to send the request to.
     * @param headers set headers only for this request
     * @param params additional GET parameters to send with the request.
     * @param responseHandler the response handler instance that should handle
     *        the response.
     */
public void get(Context context, String url, Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, null, responseHandler, context);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet)

Example 32 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project openkit-android by OpenKit.

the class RetryHandler method retryRequest.

public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;
    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());
    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;
    } else if (isInList(exceptionBlacklist, exception)) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully sent yet
        retry = true;
    }
    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }
    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        exception.printStackTrace();
    }
    return retry;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest)

Example 33 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project jersey by jersey.

the class ApacheConnector method apply.

@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
    final HttpUriRequest request = getUriHttpRequest(clientRequest);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);
    try {
        final CloseableHttpResponse response;
        final HttpClientContext context = HttpClientContext.create();
        if (preemptiveBasicAuth) {
            final AuthCache authCache = new BasicAuthCache();
            final BasicScheme basicScheme = new BasicScheme();
            authCache.put(getHost(request), basicScheme);
            context.setAuthCache(authCache);
        }
        response = client.execute(getHost(request), request, context);
        HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());
        final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null ? Statuses.from(response.getStatusLine().getStatusCode()) : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
        final ClientResponse responseContext = new ClientResponse(status, clientRequest);
        final List<URI> redirectLocations = context.getRedirectLocations();
        if (redirectLocations != null && !redirectLocations.isEmpty()) {
            responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
        }
        final Header[] respHeaders = response.getAllHeaders();
        final MultivaluedMap<String, String> headers = responseContext.getHeaders();
        for (final Header header : respHeaders) {
            final String headerName = header.getName();
            List<String> list = headers.get(headerName);
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(header.getValue());
            headers.put(headerName, list);
        }
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            if (headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
                headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
            }
            final Header contentEncoding = entity.getContentEncoding();
            if (headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
                headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
            }
        }
        try {
            responseContext.setEntityStream(new HttpClientResponseInputStream(getInputStream(response)));
        } catch (final IOException e) {
            LOGGER.log(Level.SEVERE, null, e);
        }
        return responseContext;
    } catch (final Exception e) {
        throw new ProcessingException(e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ClientResponse(org.glassfish.jersey.client.ClientResponse) BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) IOException(java.io.IOException) URI(java.net.URI) ProcessingException(javax.ws.rs.ProcessingException) IOException(java.io.IOException) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ProcessingException(javax.ws.rs.ProcessingException)

Example 34 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project FastDev4Android by jiangqqlmj.

the class HttpClientStackTest method createPutRequest.

@Test
public void createPutRequest() throws Exception {
    TestRequest.Put request = new TestRequest.Put();
    assertEquals(request.getMethod(), Method.PUT);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpPut);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPut(org.apache.http.client.methods.HttpPut) HttpPut(org.apache.http.client.methods.HttpPut) TestRequest(com.android.volley.mock.TestRequest) Test(org.junit.Test)

Example 35 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project FastDev4Android by jiangqqlmj.

the class HttpClientStackTest method createDeleteRequest.

@Test
public void createDeleteRequest() throws Exception {
    TestRequest.Delete request = new TestRequest.Delete();
    assertEquals(request.getMethod(), Method.DELETE);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpDelete);
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpDelete(org.apache.http.client.methods.HttpDelete) TestRequest(com.android.volley.mock.TestRequest) Test(org.junit.Test)

Aggregations

HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)185 Test (org.junit.Test)62 TestRequest (com.android.volley.mock.TestRequest)52 HttpGet (org.apache.http.client.methods.HttpGet)45 URI (java.net.URI)43 HttpResponse (org.apache.http.HttpResponse)41 HttpEntity (org.apache.http.HttpEntity)38 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)36 HttpPost (org.apache.http.client.methods.HttpPost)22 IOException (java.io.IOException)19 Header (org.apache.http.Header)18 JSONObject (org.json.JSONObject)18 BufferedReader (java.io.BufferedReader)17 InputStreamReader (java.io.InputStreamReader)17 PrintWriter (java.io.PrintWriter)17 StringWriter (java.io.StringWriter)17 HttpHost (org.apache.http.HttpHost)13 HttpPut (org.apache.http.client.methods.HttpPut)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 StringEntity (org.apache.http.entity.StringEntity)10