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

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

Introduction

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

Prototype

public RequestBuilder addHeader(final Header header) 

Source Link

Usage

From source file:net.ymate.framework.commons.HttpClientHelper.java

public void download(String url, ContentType contentType, String content, Header[] headers,
        final IFileHandler handler) throws Exception {
    RequestBuilder _reqBuilder = RequestBuilder.post().setUri(url).setEntity(EntityBuilder.create()
            .setContentEncoding(contentType == null || contentType.getCharset() == null ? DEFAULT_CHARSET
                    : contentType.getCharset().name())
            .setContentType(contentType).setText(content).build());
    if (headers != null && headers.length > 0) {
        for (Header _header : headers) {
            _reqBuilder.addHeader(_header);
        }/*from w w  w  . j  a  v a 2s . c  om*/
    }
    __doExecHttpDownload(_reqBuilder, handler);
}

From source file:net.ymate.framework.commons.HttpClientHelper.java

public IHttpResponse post(String url, ContentType contentType, byte[] content, Header[] headers)
        throws Exception {
    CloseableHttpClient _httpClient = __doBuildHttpClient();
    try {/*from   w ww  .  jav a  2s.com*/
        RequestBuilder _reqBuilder = RequestBuilder.post().setUri(url).setEntity(EntityBuilder.create()
                .setContentEncoding(contentType == null || contentType.getCharset() == null ? DEFAULT_CHARSET
                        : contentType.getCharset().name())
                .setContentType(contentType).setBinary(content).build());
        if (headers != null && headers.length > 0) {
            for (Header _header : headers) {
                _reqBuilder.addHeader(_header);
            }
        }
        return _httpClient.execute(_reqBuilder.build(), new ResponseHandler<IHttpResponse>() {

            public IHttpResponse handleResponse(HttpResponse response) throws IOException {
                return new IHttpResponse.NEW(response);
            }

        });
    } finally {
        _httpClient.close();
    }
}

From source file:net.ymate.framework.commons.HttpClientHelper.java

public IHttpResponse post(String url, ContentType contentType, String content, Header[] headers,
        final String defaultResponseCharset) throws Exception {
    CloseableHttpClient _httpClient = __doBuildHttpClient();
    try {// ww w. j a v a 2 s  . c  om
        RequestBuilder _reqBuilder = RequestBuilder.post().setUri(url).setEntity(EntityBuilder.create()
                .setContentEncoding(contentType == null || contentType.getCharset() == null ? DEFAULT_CHARSET
                        : contentType.getCharset().name())
                .setContentType(contentType).setText(content).build());
        if (headers != null && headers.length > 0) {
            for (Header _header : headers) {
                _reqBuilder.addHeader(_header);
            }
        }
        return _httpClient.execute(_reqBuilder.build(), new ResponseHandler<IHttpResponse>() {

            public IHttpResponse handleResponse(HttpResponse response) throws IOException {
                if (StringUtils.isNotBlank(defaultResponseCharset)) {
                    return new IHttpResponse.NEW(response, defaultResponseCharset);
                }
                return new IHttpResponse.NEW(response);
            }

        });
    } finally {
        _httpClient.close();
    }
}

From source file:net.ymate.framework.commons.HttpClientHelper.java

public IHttpResponse post(String url, ContentType contentType, InputStream content, Header[] headers,
        final String defaultResponseCharset) throws Exception {
    CloseableHttpClient _httpClient = __doBuildHttpClient();
    try {//from   w  ww .  j  a  v a  2  s.com
        RequestBuilder _reqBuilder = RequestBuilder.post().setUri(url).setEntity(EntityBuilder.create()
                .setContentEncoding(contentType == null || contentType.getCharset() == null ? DEFAULT_CHARSET
                        : contentType.getCharset().name())
                .setContentType(contentType).setStream(content).build());
        if (headers != null && headers.length > 0) {
            for (Header _header : headers) {
                _reqBuilder.addHeader(_header);
            }
        }
        return _httpClient.execute(_reqBuilder.build(), new ResponseHandler<IHttpResponse>() {

            public IHttpResponse handleResponse(HttpResponse response) throws IOException {
                if (StringUtils.isNotBlank(defaultResponseCharset)) {
                    return new IHttpResponse.NEW(response, defaultResponseCharset);
                }
                return new IHttpResponse.NEW(response);
            }

        });
    } finally {
        _httpClient.close();
    }
}

From source file:com.hp.octane.integrations.services.rest.OctaneRestClientImpl.java

/**
 * This method should be the ONLY mean that creates Http Request objects
 *
 * @param octaneRequest Request data as it is maintained in Octane related flavor
 * @return pre-configured HttpUriRequest
 *//*from  w w  w.j  a  v a 2  s.  co m*/
private HttpUriRequest createHttpRequest(OctaneRequest octaneRequest) {
    HttpUriRequest request;
    RequestBuilder requestBuilder;

    //  create base request by METHOD
    if (octaneRequest.getMethod().equals(HttpMethod.GET)) {
        requestBuilder = RequestBuilder.get(octaneRequest.getUrl());
    } else if (octaneRequest.getMethod().equals(HttpMethod.DELETE)) {
        requestBuilder = RequestBuilder.delete(octaneRequest.getUrl());
    } else if (octaneRequest.getMethod().equals(HttpMethod.POST)) {
        requestBuilder = RequestBuilder.post(octaneRequest.getUrl());
        requestBuilder
                .addHeader(new BasicHeader(RestService.CONTENT_ENCODING_HEADER, RestService.GZIP_ENCODING));
        requestBuilder.setEntity(new GzipCompressingEntity(
                new InputStreamEntity(octaneRequest.getBody(), ContentType.APPLICATION_JSON)));
    } else if (octaneRequest.getMethod().equals(HttpMethod.PUT)) {
        requestBuilder = RequestBuilder.put(octaneRequest.getUrl());
        requestBuilder
                .addHeader(new BasicHeader(RestService.CONTENT_ENCODING_HEADER, RestService.GZIP_ENCODING));
        requestBuilder.setEntity(new GzipCompressingEntity(
                new InputStreamEntity(octaneRequest.getBody(), ContentType.APPLICATION_JSON)));
    } else {
        throw new RuntimeException("HTTP method " + octaneRequest.getMethod() + " not supported");
    }

    //  set custom headers
    if (octaneRequest.getHeaders() != null) {
        for (Map.Entry<String, String> e : octaneRequest.getHeaders().entrySet()) {
            requestBuilder.setHeader(e.getKey(), e.getValue());
        }
    }

    //  set system headers
    requestBuilder.setHeader(CLIENT_TYPE_HEADER, CLIENT_TYPE_VALUE);

    request = requestBuilder.build();
    return request;
}

From source file:net.ymate.framework.commons.HttpClientHelper.java

public IHttpResponse post(String url, ContentType contentType, Map<String, String> params, Header[] headers,
        final String defaultResponseCharset) throws Exception {
    CloseableHttpClient _httpClient = __doBuildHttpClient();
    try {// ww  w  . j a  v a2 s.c o m
        RequestBuilder _reqBuilder = RequestBuilder.post().setUri(url).setEntity(EntityBuilder.create()
                .setContentType(contentType)
                .setContentEncoding(contentType == null || contentType.getCharset() == null ? DEFAULT_CHARSET
                        : contentType.getCharset().name())
                .setParameters(__doBuildNameValuePairs(params)).build());
        if (headers != null && headers.length > 0) {
            for (Header _header : headers) {
                _reqBuilder.addHeader(_header);
            }
        }
        return _httpClient.execute(_reqBuilder.build(), new ResponseHandler<IHttpResponse>() {

            public IHttpResponse handleResponse(HttpResponse response) throws IOException {
                if (StringUtils.isNotBlank(defaultResponseCharset)) {
                    return new IHttpResponse.NEW(response, defaultResponseCharset);
                }
                return new IHttpResponse.NEW(response);
            }

        });
    } finally {
        _httpClient.close();
    }
}

From source file:nya.miku.wishmaster.http.streamer.HttpStreamer.java

/**
 * HTTP ?  ?. ? ?  ? ?,    release()  !//from  ww  w  .  j  a  v  a2  s . c  om
 * ?   ? ?? ? ?  If-Modified-Since,  ?      
 *  ? ?? ?    Modified ({@link #removeFromModifiedMap(String)})!
 * @param url ? ?
 * @param requestModel  ? (  null,   GET   If-Modified)
 * @param httpClient HTTP , ?? ?
 * @param listener ? ?? ?? (  null)
 * @param task ,     (  null)
 * @return  ?     HTTP 
 * @throws HttpRequestException ?, ?  ?  ?
 */
public HttpResponseModel getFromUrl(String url, HttpRequestModel requestModel, HttpClient httpClient,
        ProgressListener listener, CancellableTask task) throws HttpRequestException {
    if (requestModel == null)
        requestModel = HttpRequestModel.builder().setGET().build();

    // Request
    HttpUriRequest request = null;
    try {
        RequestConfig requestConfigBuilder = ExtendedHttpClient
                .getDefaultRequestConfigBuilder(requestModel.timeoutValue)
                .setRedirectsEnabled(!requestModel.noRedirect).build();

        RequestBuilder requestBuilder = null;
        switch (requestModel.method) {
        case HttpRequestModel.METHOD_GET:
            requestBuilder = RequestBuilder.get().setUri(url);
            break;
        case HttpRequestModel.METHOD_POST:
            requestBuilder = RequestBuilder.post().setUri(url).setEntity(requestModel.postEntity);
            break;
        default:
            throw new IllegalArgumentException("Incorrect type of HTTP Request");
        }
        if (requestModel.customHeaders != null) {
            for (Header header : requestModel.customHeaders) {
                requestBuilder.addHeader(header);
            }
        }
        if (requestModel.checkIfModified && requestModel.method == HttpRequestModel.METHOD_GET) {
            synchronized (ifModifiedMap) {
                if (ifModifiedMap.containsKey(url)) {
                    requestBuilder
                            .addHeader(new BasicHeader(HttpHeaders.IF_MODIFIED_SINCE, ifModifiedMap.get(url)));
                }
            }
        }
        request = requestBuilder.setConfig(requestConfigBuilder).build();
    } catch (Exception e) {
        Logger.e(TAG, e);
        HttpResponseModel.release(request, null);
        throw new IllegalArgumentException(e);
    }
    // ?
    HttpResponseModel responseModel = new HttpResponseModel();
    HttpResponse response = null;
    try {
        IOException responseException = null;
        for (int i = 0; i < 5; ++i) {
            try {
                if (task != null && task.isCancelled())
                    throw new InterruptedException();
                response = httpClient.execute(request);
                responseException = null;
                break;
            } catch (IOException e) {
                Logger.e(TAG, e);
                responseException = e;
                if (e.getMessage() == null)
                    break;
                if (e.getMessage().indexOf("Connection reset by peer") != -1
                        || e.getMessage().indexOf("I/O error during system call, Broken pipe") != -1) {
                    continue;
                } else {
                    break;
                }
            }
        }
        if (responseException != null) {
            throw responseException;
        }
        if (task != null && task.isCancelled())
            throw new InterruptedException();
        //  ???? HTTP
        StatusLine status = response.getStatusLine();
        responseModel.statusCode = status.getStatusCode();
        responseModel.statusReason = status.getReasonPhrase();
        //   (headers)
        String lastModifiedValue = null;
        if (responseModel.statusCode == 200) {
            Header header = response.getFirstHeader(HttpHeaders.LAST_MODIFIED);
            if (header != null)
                lastModifiedValue = header.getValue();
        }
        Header header = response.getFirstHeader(HttpHeaders.LOCATION);
        if (header != null)
            responseModel.locationHeader = header.getValue();
        responseModel.headers = response.getAllHeaders();
        //
        HttpEntity responseEntity = response.getEntity();
        if (responseEntity != null) {
            responseModel.contentLength = responseEntity.getContentLength();
            if (listener != null)
                listener.setMaxValue(responseModel.contentLength);
            InputStream stream = responseEntity.getContent();
            responseModel.stream = IOUtils.modifyInputStream(stream, listener, task);
        }
        responseModel.request = request;
        responseModel.response = response;
        if (lastModifiedValue != null) {
            synchronized (ifModifiedMap) {
                ifModifiedMap.put(url, lastModifiedValue);
            }
        }
    } catch (Exception e) {
        Logger.e(TAG, e);
        HttpResponseModel.release(request, response);
        throw new HttpRequestException(e);
    }

    return responseModel;
}