Example usage for org.apache.http.client.methods HttpUriRequest addHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest addHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest addHeader.

Prototype

void addHeader(String str, String str2);

Source Link

Usage

From source file:org.hawkular.apm.tests.agent.opentracing.client.http.ApacheHttpClientITest.java

protected void testHttpClientWithoutResponseHandler(HttpUriRequest request, boolean fault, boolean respexpected)
        throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/* w ww. j  a v a 2s  .  c o m*/
        request.addHeader("test-header", "test-value");
        if (fault) {
            request.addHeader("test-fault", "true");
        }
        if (!respexpected) {
            request.addHeader("test-no-data", "true");
        }

        HttpResponse response = httpclient.execute(request);

        int status = response.getStatusLine().getStatusCode();

        if (!fault) {
            assertEquals("Unexpected response code", 200, status);

            if (respexpected) {
                HttpEntity entity = response.getEntity();

                assertNotNull(entity);

                assertEquals(HELLO_WORLD, EntityUtils.toString(entity));
            }
        } else {
            assertEquals("Unexpected fault response code", 401, status);
        }

    } catch (ConnectException ce) {
        assertEquals(BAD_URL, request.getURI().toString());
    } finally {
        httpclient.close();
    }

    Wait.until(() -> getTracer().finishedSpans().size() == 1);

    List<MockSpan> spans = getTracer().finishedSpans();
    assertEquals(1, spans.size());
    assertEquals(Tags.SPAN_KIND_CLIENT, spans.get(0).tags().get(Tags.SPAN_KIND.getKey()));
    assertEquals(request.getMethod(), spans.get(0).operationName());
    assertEquals(request.getURI().toString(), spans.get(0).tags().get(Tags.HTTP_URL.getKey()));
    if (fault) {
        assertEquals("401", spans.get(0).tags().get(Tags.HTTP_STATUS.getKey()));
    }
}

From source file:com.github.tomakehurst.wiremock.client.HttpAdminClient.java

private String safelyExecuteRequest(String url, HttpUriRequest request) {
    if (hostHeader != null) {
        request.addHeader(HOST, hostHeader);
    }/*ww w. j  a va 2s . c o  m*/

    try (CloseableHttpResponse response = httpClient.execute(request)) {
        int statusCode = response.getStatusLine().getStatusCode();
        if (HttpStatus.isServerError(statusCode)) {
            throw new VerificationException("Expected status 2xx for " + url + " but was " + statusCode);
        }

        String body = getEntityAsStringAndCloseStream(response);
        if (HttpStatus.isClientError(statusCode)) {
            Errors errors = Json.read(body, Errors.class);
            throw ClientError.fromErrors(errors);
        }

        return body;
    } catch (Exception e) {
        return throwUnchecked(e, String.class);
    }
}

From source file:org.changhong.sync.web.WebConnection.java

protected HttpResponse execute(HttpUriRequest request) throws UnknownHostException {

    DefaultHttpClient httpclient = MySSLSocketFactory.getNewHttpClient();

    try {/*  w  w w  .jav  a  2  s.  c o m*/
        // Execute the request
        TLog.i(TAG, "Sending http-header: {0}: {1}", "X-Tomboy-Client", Tomdroid.HTTP_HEADER);
        request.addHeader("X-Tomboy-Client", Tomdroid.HTTP_HEADER);
        HttpResponse response = httpclient.execute(request);
        return response;

    } catch (UnknownHostException e) {
        throw e;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        TLog.i(TAG, "Somethings wrong with your HTTP request. Maybe errors in SSL, certificate?");
        e.printStackTrace();
    }

    return null;
}

From source file:org.jclouds.http.apachehc.ApacheHCHttpCommandExecutorService.java

@Override
protected HttpUriRequest convert(HttpRequest request) throws IOException {
    HttpUriRequest returnVal = apacheHCUtils.convertToApacheRequest(request);
    if (request.getPayload() != null && request.getPayload().getContentMetadata().getContentMD5() != null) {
        String md5 = base64()//  w  ww.  j a  va  2 s .  c  o m
                .encode(request.getPayload().getContentMetadata().getContentMD5AsHashCode().asBytes());
        returnVal.addHeader("Content-MD5", md5);
    }

    if (!returnVal.containsHeader(HttpHeaders.USER_AGENT)) {
        returnVal.addHeader(HttpHeaders.USER_AGENT, userAgent);
    }

    return returnVal;
}

From source file:lynxtools.async_download.AsyncHttpClient.java

protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest,
        String contentType, AsyncHttpResponseHandler responseHandler) {
    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }// w w w. j ava  2  s . c om

    request = threadPool.submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));
}

From source file:com.microsoft.windowsazure.messaging.Connection.java

/**
 * Adds the Authorization header to a request
 * @param request   The request to modify
 * @throws java.security.InvalidKeyException
 *//*from  w  w  w.  j  av  a  2 s .  co  m*/
private void addAuthorizationHeader(HttpUriRequest request) throws InvalidKeyException {
    String token = generateAuthToken(request.getURI().toString());

    request.addHeader(AUTHORIZATION_HEADER, token);
}

From source file:com.fujitsu.dc.test.jersey.DcRestAdapter.java

/**
 * ????GET.//from   w  w  w.ja va2 s. c  o m
 * @param url URL
 * @param headers ??
 * @return DcResponse
 * @throws DcException DAO
 */
public final DcResponse getAcceptEncodingGzip(final String url, final HashMap<String, String> headers)
        throws DcException {
    HttpUriRequest req = new HttpGet(url);
    req.addHeader("Accept-Encoding", "gzip");
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        req.setHeader(entry.getKey(), entry.getValue());
    }
    req.addHeader("X-Dc-Version", DcCoreTestConfig.getCoreVersion());

    debugHttpRequest(req, "");
    DcResponse res = this.request(req);
    return res;
}

From source file:io.personium.test.jersey.PersoniumRestAdapter.java

/**
 * ????GET.//from  w  ww.  j  av a2s  . co  m
 * @param url URL
 * @param headers ??
 * @return DcResponse
 * @throws PersoniumException DAO
 */
public final PersoniumResponse getAcceptEncodingGzip(final String url, final HashMap<String, String> headers)
        throws PersoniumException {
    HttpUriRequest req = new HttpGet(url);
    req.addHeader("Accept-Encoding", "gzip");
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        req.setHeader(entry.getKey(), entry.getValue());
    }
    req.addHeader("X-Personium-Version", PersoniumCoreTestConfig.getCoreVersion());

    debugHttpRequest(req, "");
    PersoniumResponse res = this.request(req);
    return res;
}

From source file:org.eclipse.rcptt.internal.testrail.APIClient.java

private void setUpHeaders(HttpUriRequest request) {
    String credentials = username + ":" + password;
    String encodedCredentials = Base64.encode(credentials.getBytes(StandardCharsets.ISO_8859_1));
    String authorizationHeader = "Basic " + new String(encodedCredentials);
    request.setHeader(HttpHeaders.AUTHORIZATION, authorizationHeader);

    String contentTypeHeader = "application/json";
    request.addHeader(HttpHeaders.CONTENT_TYPE, contentTypeHeader);
}