Java Code Examples for org.apache.http.client.methods.HttpRequestBase#getHeaders()

The following examples show how to use org.apache.http.client.methods.HttpRequestBase#getHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApacheHttpRequestFactoryTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void ceateSetsHostHeaderByDefault() {
    SdkHttpRequest sdkRequest = SdkHttpRequest.builder()
            .uri(URI.create("http://localhost:12345/"))
            .method(SdkHttpMethod.HEAD)
            .build();
    HttpExecuteRequest request = HttpExecuteRequest.builder()
            .request(sdkRequest)
            .build();
    HttpRequestBase result = instance.create(request, requestConfig);
    Header[] hostHeaders = result.getHeaders(HttpHeaders.HOST);
    assertNotNull(hostHeaders);
    assertEquals(1, hostHeaders.length);
    assertEquals("localhost:12345", hostHeaders[0].getValue());
}
 
Example 2
Source File: ApacheHttpRequestFactoryTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultHttpPortsAreNotInDefaultHostHeader() {
    SdkHttpRequest sdkRequest = SdkHttpRequest.builder()
            .uri(URI.create("http://localhost:80/"))
            .method(SdkHttpMethod.HEAD)
            .build();
    HttpExecuteRequest request = HttpExecuteRequest.builder()
            .request(sdkRequest)
            .build();
    HttpRequestBase result = instance.create(request, requestConfig);
    Header[] hostHeaders = result.getHeaders(HttpHeaders.HOST);
    assertNotNull(hostHeaders);
    assertEquals(1, hostHeaders.length);
    assertEquals("localhost", hostHeaders[0].getValue());

    sdkRequest = SdkHttpRequest.builder()
            .uri(URI.create("https://localhost:443/"))
            .method(SdkHttpMethod.HEAD)
            .build();
    request = HttpExecuteRequest.builder()
            .request(sdkRequest)
            .build();
    result = instance.create(request, requestConfig);
    hostHeaders = result.getHeaders(HttpHeaders.HOST);
    assertNotNull(hostHeaders);
    assertEquals(1, hostHeaders.length);
    assertEquals("localhost", hostHeaders[0].getValue());
}
 
Example 3
Source File: ApacheHttpRequestFactory.java    From ibm-cos-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Configures the headers in the specified Apache HTTP request.
 */
private void addHeadersToRequest(HttpRequestBase httpRequest, Request<?> request) {

    httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request.getEndpoint()));

    // Copy over any other headers already in our request
    for (Entry<String, String> entry : request.getHeaders().entrySet()) {
        /*
         * HttpClient4 fills in the Content-Length header and complains if
         * it's already present, so we skip it here. We also skip the Host
         * header to avoid sending it twice, which will interfere with some
         * signing schemes.
         */
        if (!(ignoreHeaders.contains(entry.getKey()))) {
            httpRequest.addHeader(entry.getKey(), entry.getValue());
        }
    }

    /* Set content type and encoding */
    if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest
            .getHeaders
                    (HttpHeaders.CONTENT_TYPE).length == 0) {
        httpRequest.addHeader(HttpHeaders.CONTENT_TYPE,
                "application/x-www-form-urlencoded; " +
                        "charset=" + DEFAULT_ENCODING.toLowerCase());
    }
}
 
Example 4
Source File: HttpRequestFactory.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private void configureHeaders(HttpRequestBase httprequestbase, BasicGalaxyRequest basicgalaxyrequest, ExecutionContext executioncontext, ClientConfiguration clientconfiguration)
{
    URI uri = basicgalaxyrequest.getEndpoint();
    String s = uri.getHost();
    if (HttpUtils.isUsingNonDefaultPort(uri))
    {
        s = (new StringBuilder()).append(s).append(":").append(uri.getPort()).toString();
    }
    httprequestbase.addHeader("Host", s);
    Iterator iterator = basicgalaxyrequest.getHeaders().entrySet().iterator();
    do
    {
        if (!iterator.hasNext())
        {
            break;
        }
        java.util.Map.Entry entry = (java.util.Map.Entry)iterator.next();
        if (!((String)entry.getKey()).equalsIgnoreCase("Content-Length") && !((String)entry.getKey()).equalsIgnoreCase("Host"))
        {
            httprequestbase.addHeader((String)entry.getKey(), (String)entry.getValue());
        }
    } while (true);
    if (httprequestbase.getHeaders("Content-Type") == null || httprequestbase.getHeaders("Content-Type").length == 0)
    {
        httprequestbase.addHeader("Content-Type", (new StringBuilder()).append("application/x-www-form-urlencoded; charset=").append("UTF-8".toLowerCase()).toString());
    }
    if (executioncontext != null && executioncontext.getContextUserAgent() != null)
    {
        httprequestbase.addHeader("User-Agent", createUserAgentString(clientconfiguration, executioncontext.getContextUserAgent()));
    }
}
 
Example 5
Source File: BaseRequest.java    From r2m-plugin-android with Apache License 2.0 5 votes vote down vote up
@Override
public void doWork() throws Exception {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpRequestBase request = getRequest(requestModel);

    List<RequestHeaderModel> requestModelHeaders = requestModel.getHeaders();

    for (RequestHeaderModel header : requestModelHeaders) {
        request.setHeader(header.getName(), header.getValue());
    }

    // If not specified as a header argument, Content-Type is inferred from the request body
    // Add the header here so the request is successful.
    if (request.getHeaders(ContentTypeHelper.CONTENT_TYPE_HEADER) == null ||
            request.getHeaders(ContentTypeHelper.CONTENT_TYPE_HEADER).length == 0) {
        RestContentType type = ExampleParser.guessContentType(requestModel.getRequest());
        if (null != type) {
            request.setHeader(ContentTypeHelper.CONTENT_TYPE_HEADER, type.getName());
        }
    }
    // check if content-type is parameterized
    HttpResponse httpResponse = httpClient.execute(request);
    ApiMethodModel methodModel = new ApiMethodModel();
    methodModel.setRequestHeaders(request.getAllHeaders());
    methodModel.setHttpResponse(httpResponse);
    methodModel.setRequestModel(requestModel);
    onSuccess(methodModel);

}