private static void userAgent(HttpUriRequest httpUriRequest){ httpUriRequest.addHeader(userAgentHeader); } }
HttpUriRequest request = new HttpGet(url); request.addHeader("Accept-Encoding", "gzip"); // ... httpClient.execute(request);
/** * Add the given headers to the given HTTP request. * @param httpRequest the request to add the headers to * @param headers the headers to add */ static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) { headers.forEach((headerName, headerValues) -> { if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265 String headerValue = StringUtils.collectionToDelimitedString(headerValues, "; "); httpRequest.addHeader(headerName, headerValue); } else if (!HTTP.CONTENT_LEN.equalsIgnoreCase(headerName) && !HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName)) { for (String headerValue : headerValues) { httpRequest.addHeader(headerName, headerValue); } } }); }
private static Map<String, String> writeOutBoundHeaders(final MultivaluedMap<String, Object> headers, final HttpUriRequest request) { final Map<String, String> stringHeaders = HeaderUtils.asStringHeadersSingleValue(headers); for (final Map.Entry<String, String> e : stringHeaders.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } return stringHeaders; }
/** * Helper method that injects an authentication token to send with the method. * @param method method to inject the authentication token into. * @param token authentication token to inject. */ private void injectToken(HttpUriRequest method, AuthenticatedURL.Token token) { String t = token.toString(); if (t != null) { if (!t.startsWith("\"")) { t = "\"" + t + "\""; } method.addHeader(COOKIE, AUTH_COOKIE_EQ + t); } }
/** * Add the given headers to the given HTTP request. * @param httpRequest the request to add the headers to * @param headers the headers to add */ static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) { headers.forEach((headerName, headerValues) -> { if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265 String headerValue = StringUtils.collectionToDelimitedString(headerValues, "; "); httpRequest.addHeader(headerName, headerValue); } else if (!HTTP.CONTENT_LEN.equalsIgnoreCase(headerName) && !HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName)) { for (String headerValue : headerValues) { httpRequest.addHeader(headerName, headerValue); } } }); }
public <T> T post(String url, Object body, Map<String, String> headers, ResponseHandler<T> responseHandler, int... statusCodes) throws IOException { HttpUriRequest request = newPost(url, body); for (Entry<String, String> entry : headers.entrySet()) { request.addHeader(entry.getKey(), entry.getValue()); } return httpClient.execute(request, new StatusCodeCheckerResponseHandler<>(responseHandler, statusCodes)); }
protected <T extends JestResult> HttpUriRequest prepareRequest(final Action<T> clientRequest, final RequestConfig requestConfig) { String elasticSearchRestUrl = getRequestURL(getNextServer(), clientRequest.getURI(elasticsearchVersion)); HttpUriRequest request = constructHttpMethod(clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData(gson), requestConfig); log.debug("Request method={} url={}", clientRequest.getRestMethodName(), elasticSearchRestUrl); // add headers added to action for (Entry<String, Object> header : clientRequest.getHeaders().entrySet()) { request.addHeader(header.getKey(), header.getValue().toString()); } return request; }
private HttpUriRequest convertHttpUriRequest(Request request, Site site, Proxy proxy) { RequestBuilder requestBuilder = selectRequestMethod(request).setUri(UrlUtils.fixIllegalCharacterInUrl(request.getUrl())); if (site.getHeaders() != null) { for (Map.Entry<String, String> headerEntry : site.getHeaders().entrySet()) { requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue()); } } RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); if (site != null) { requestConfigBuilder.setConnectionRequestTimeout(site.getTimeOut()) .setSocketTimeout(site.getTimeOut()) .setConnectTimeout(site.getTimeOut()) .setCookieSpec(CookieSpecs.STANDARD); } if (proxy != null) { requestConfigBuilder.setProxy(new HttpHost(proxy.getHost(), proxy.getPort())); } requestBuilder.setConfig(requestConfigBuilder.build()); HttpUriRequest httpUriRequest = requestBuilder.build(); if (request.getHeaders() != null && !request.getHeaders().isEmpty()) { for (Map.Entry<String, String> header : request.getHeaders().entrySet()) { httpUriRequest.addHeader(header.getKey(), header.getValue()); } } return httpUriRequest; }
public <T extends JestResult> T execute(Action<T> clientRequest, RequestConfig requestConfig) throws IOException { String elasticSearchRestUrl = getRequestURL(getNextServer(), clientRequest.getURI(ElasticsearchVersion.UNKNOWN)); HttpUriRequest request = constructHttpMethod(clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData(gson), requestConfig); // add headers added to action if (!clientRequest.getHeaders().isEmpty()) { for (Map.Entry<String, Object> header : clientRequest.getHeaders().entrySet()) { request.addHeader(header.getKey(), header.getValue().toString()); } } try { HttpResponse response = httpClient.execute(request); return deserializeResponse(response, clientRequest); } catch (HttpHostConnectException ex) { throw new CouldNotConnectException(ex.getHost().toURI(), ex); } }
public HttpResponse executeRequest(HttpUriRequest uriRequest, Header... header) throws Exception { CloseableHttpClient httpClient = null; try { httpClient = getHTTPClient(); if (header != null && header.length > 0) { for (int i = 0; i < header.length; i++) { Header h = header[i]; uriRequest.addHeader(h); } } if (!uriRequest.containsHeader("Content-Type")) { uriRequest.addHeader("Content-Type","application/json"); } HttpResponse res = new HttpResponse(httpClient.execute(uriRequest)); log.debug(res.getBody()); return res; } finally { if (httpClient != null) { httpClient.close(); } } }
HttpUriRequest request = new HttpGet(url); request.addHeader("Content-Encoding", "gzip"); // ... httpClient.execute(request);
continue; httpRequest.addHeader(header.getKey(), header.getValue());
private static Map<String, String> writeOutBoundHeaders(final MultivaluedMap<String, Object> headers, final HttpUriRequest request) { final Map<String, String> stringHeaders = HeaderUtils.asStringHeadersSingleValue(headers); for(final Map.Entry<String, String> e : stringHeaders.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } return stringHeaders; }
@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().encode(asByteSource(request.getPayload().getInput()).hash(md5()).asBytes()); returnVal.addHeader("Content-MD5", md5); } return returnVal; }