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

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

Introduction

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

Prototype

void setHeaders(Header[] headerArr);

Source Link

Usage

From source file:com.bigdata.rdf.sail.webapp.AbstractProtocolTest.java

/**
 * This is the main entry point for subclasses.
 * This method sends a request to the server, as set up
 * by setABC methods, and returns the string send back to the client.
 * @param paramValues This is an even number of param [=] value pairs. Multiple values for the same param are supported.
 *    These are passed to the server either as URL query params, or as URL encoded values in the body if the method
 *    {@link #setMethodisPostUrlEncodedData()} has been called.
 * @return the data returned by the server.
 * @throws IOException//w w  w  . j  av a  2s  .c om
 */
protected String serviceRequest(final String... paramValues) throws IOException {
    HttpUriRequest req;
    responseContentType = null;
    try {
        try {
            req = requestFactory.createRequest(paramValues);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
        req.setHeader("Accept", accept == null ? "*" : accept);

        if (headers != null) {
            req.setHeaders(headers);
        }

        final HttpResponse resp = client.execute(req);
        String page = "";
        final HttpEntity entity = resp.getEntity();
        if (entity != null) {
            String encoding = "utf-8";
            assertNotNull(
                    "Entity in " + resp.getStatusLine().getStatusCode() + " response must specify content type",
                    entity.getContentType());
            final Matcher m = charset.matcher(entity.getContentType().getValue());
            if (m.find()) {
                encoding = m.group(1);
            }
            page = QueryServlet.readFully(new InputStreamReader(entity.getContent(), encoding));
            responseContentType = entity.getContentType().getValue();
        }
        if (resp.getStatusLine().getStatusCode() >= (permit400s ? 500 : 400)) {
            fail(resp.getStatusLine().toString() + "\n" + page);
        }
        return page;
    } finally {
        resetDefaultOptions();
    }
}

From source file:com.subgraph.vega.internal.http.proxy.ProxyRequestHandler.java

private HttpUriRequest copyToUriRequest(HttpRequest request) throws ProtocolException {
    URI uri;//ww w.  ja  v a 2  s  . com
    try {
        uri = new URI(request.getRequestLine().getUri());
    } catch (URISyntaxException e) {
        throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(), e);
    }
    // ensuring we have scheme and host also prevents the proxy from connecting back to itself
    if (uri.getScheme() == null) {
        throw new ProtocolException("No scheme in proxy request URI");
    }
    if (uri.getHost() == null) {
        throw new ProtocolException("No host in proxy request URI");
    }

    final HttpUriRequest uriRequest;
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingMutableRequest tmp = new HttpEntityEnclosingMutableRequest(
                request.getRequestLine().getMethod(), uri);
        tmp.setEntity(copyEntity(((HttpEntityEnclosingRequest) request).getEntity()));
        uriRequest = tmp;
    } else {
        uriRequest = new HttpMutableRequest(request.getRequestLine().getMethod(), uri);
    }
    uriRequest.setParams(request.getParams());
    uriRequest.setHeaders(request.getAllHeaders());
    return uriRequest;
}

From source file:cn.kuwo.sing.phone4tv.commons.http.AsyncHttpClient.java

public void get(Context context, String url, Header[] headers, RequestParams params, String encoding,
        AsyncHttpResponseHandler responseHandler) {
    HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params, encoding));
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, null, responseHandler, context);
}

From source file:com.chuannuo.tangguo.net.TGHttpClient.java

/**
 * Perform a HTTP GET request and track the Android Context which initiated
 * the request with customized headers/* w w w . java  2  s  . c om*/
 * 
 * @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,
        TGHttpResponseHandler responseHandler) {
    HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, null, responseHandler, context);
}

From source file:com.LaunchKeyManager.http.AsyncHttpClient.java

/**
 * Perform a HTTP GET request and track the Android Context which initiated
 * the request with customized headers/* w w  w  .j a v a2s.com*/
 *
 * @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);
}

From source file:com.frand.easyandroid.http.FFHttpClient.java

/**
 * Perform a HTTP GET request and track the Android Context which initiated
 * the request with customized headers//from  w w  w  . j  a v a2  s .c o m
 * 
 * @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, int reqTag, String url, Header[] headers, FFRequestParams params,
        FFHttpRespHandler responseHandler) {
    HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, null, responseHandler, context, reqTag,
            getUrlWithQueryString(url, params));
}

From source file:com.pyj.http.AsyncHttpClient.java

/**
 * Perform a HTTP GET request and track the Android Context which initiated
 * the request with customized headers//  w  ww  .  java  2  s . c  o m
 * 
 * @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) {
    if (url == null)
        responseHandler.sendFailureMessage(new Throwable("??"), "??",
                responseHandler.reqCode);
    else {
        HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
        if (headers != null)
            request.setHeaders(headers);
        sendRequest(httpClient, httpContext, request, null, responseHandler, context);
    }

}

From source file:mobi.dlys.android.core.net.http.client.AsyncHttpClient.java

public void get(Context context, String url, Header[] headers, RequestParams params,
        AsyncHttpResponseHandler responseHandler, int connectionTimeout, int soTimeout) {
    try {//www .  j ava2s  . c  om
        HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
        if (headers != null)
            request.setHeaders(headers);
        HttpParams httpParams = request.getParams();
        if (null == httpParams) {
            httpParams = new BasicHttpParams();
        }
        HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout); // 
        HttpConnectionParams.setSoTimeout(httpParams, soTimeout); // 
        sendRequest(httpClient, httpContext, request, null, responseHandler, context);
    } catch (Exception e) {
        e.printStackTrace();
        responseHandler.sendFailureMessage(e, "");
    }
}

From source file:com.waltz3d.common.httpclient.AsyncHttpClient.java

/**
 * Perform a HTTP GET request and track the Android Context which initiated
 * the request with customized headers/*w w  w  .  j  a  v a2 s  .c  o  m*/
 * 
 * @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) {
    try {
        HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
        if (headers != null)
            request.setHeaders(headers);
        sendRequest(httpClient, httpContext, request, null, responseHandler, context);
    } catch (Exception e) {
        e.printStackTrace();
        responseHandler.sendFailureMessage(e, "");
    }
}

From source file:org.apache.sling.testing.clients.AbstractSlingClient.java

/**
 * <p>Executes an HTTP request, WITHOUT consuming the entity in the response. The caller is responsible for consuming the entity or
 * closing the response's InputStream in order to release the connection.
 * Otherwise, the client might run out of connections and will block</p>
 *
 * <p><b>Use this with caution and only if necessary for streaming</b>, otherwise use the safe method
 * {@link #doRequest(HttpUriRequest, List, int...)}</p>
 *
 * <p>Adds the headers and checks the response against expected status</p>
 *
 * @param request the request to be executed
 * @param headers optional headers to be added to the request
 * @param expectedStatus if passed, the response status is checked against it/them, and has to match at least one of them
 * @return the response, with the entity not consumed
 * @throws ClientException if the request could not be executed
 *///from   ww w . j a va2  s .  c o  m
public SlingHttpResponse doStreamRequest(HttpUriRequest request, List<Header> headers, int... expectedStatus)
        throws ClientException {
    // create context from config
    HttpClientContext context = createHttpClientContextFromConfig();

    // add headers
    if (headers != null) {
        request.setHeaders(headers.toArray(new Header[headers.size()]));
    }

    try {
        log.debug("request {} {}", request.getMethod(), request.getURI());
        SlingHttpResponse response = new SlingHttpResponse(this.execute(request, context));
        log.debug("response {}", HttpUtils.getHttpStatus(response));
        // Check the status and throw a ClientException if it doesn't match expectedStatus, but close the entity before
        if (expectedStatus != null && expectedStatus.length > 0) {
            try {
                HttpUtils.verifyHttpStatus(response, expectedStatus);
            } catch (ClientException e) {
                // catch the exception to make sure we close the entity before re-throwing it
                response.close();
                throw e;
            }
        }

        return response;
    } catch (IOException e) {
        throw new ClientException("Could not execute http request", e);
    }
}