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:io.fabric8.maven.docker.access.util.RequestUtil.java

public static HttpUriRequest addDefaultHeaders(HttpUriRequest req) {
    req.addHeader(HEADER_ACCEPT, HEADER_ACCEPT_ALL);
    req.addHeader("Content-Type", "application/json");
    return req;/*from w ww .ja v  a  2s  .c o  m*/
}

From source file:zz.pseas.ghost.utils.DownloadUtil.java

public static void setUserAgent(HttpUriRequest request) {
    request.addHeader("User-Agent",
            "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");

    //request.addHeader("Host","gd.189.cn");
    //request.addHeader("Content-Type","text/plain");
    //request.addHeader("Referer","http://gd.189.cn/common/login.jsp?UATicket=-1&loginOldUri=null");
    //request.addHeader("host","my.alipay.com");
    //request.addHeader("User-Agent","Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
    //request.addHeader("Host","api.m.taobao.com");
}

From source file:Main.java

private static void addGoogleAuthHeader(HttpUriRequest request, String token) {
    if (token != null) {
        Log.i(LOG_TAG, "adding auth token token: " + token);
        request.addHeader("Authorization", AUTHORIZATION_HEADER_PREFIX + token);
    }/*w ww  .ja v  a  2  s . c  om*/
}

From source file:com.blacklocus.jres.http.HttpMethods.java

/**
 * @param method  http method, case-insensitive
 * @param url     destination of request
 * @param payload (optional) request body. An InputStream or String will be sent as is, while any other type will
 *                be serialized with {@link ObjectMappers#NORMAL} to JSON.
 * @return HttpUriRequest with header <code>Accept: application/json; charset=UTF-8</code>
 *//*from   w ww.  ja  v  a2  s  . co m*/
public static HttpUriRequest createRequest(String method, String url, final Object payload) {

    LOG.debug("{} {}", method, url);

    HttpUriRequest httpUriRequest = METHODS.get(method.toUpperCase()).newMethod(url);
    httpUriRequest.addHeader("Accept", ContentType.APPLICATION_JSON.toString());

    if (payload != null) {
        try {

            // This cast will except if a body is given for non-HttpEntityEnclosingRequest types. Deal with it later.
            ((HttpEntityEnclosingRequest) httpUriRequest).setEntity(createEntity(payload));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return httpUriRequest;
}

From source file:Main.java

private static void setHeader(HttpUriRequest request, Map<String, String> headers) {
    if (headers == null || headers.size() == 0) {
        throw new NullPointerException("headers not be null");
    }// w ww.  ja  v  a 2  s.  c  o  m
    for (Entry<String, String> entry : headers.entrySet()) {
        request.addHeader(entry.getKey(), entry.getValue());
    }
}

From source file:spark.protocol.SparqlCall.java

/**
 * Add headers to a request./*from   ww w .j a v a  2 s.  co m*/
 * @param req The request to set the headers on.
 */
static void addHeaders(HttpUriRequest req, String mimeType) {
    if (POST.equalsIgnoreCase(req.getMethod())) {
        req.addHeader(CONTENT_TYPE, FORM_ENCODED);
    }
    if (mimeType != null)
        req.setHeader(ACCEPT, mimeType);
}

From source file:com.mber.client.HTTParty.java

private static Call execute(final HttpUriRequest request) throws IOException {
    HttpClient client = new DefaultHttpClient();
    try {//  w  w  w  .  j  a  va  2 s .  co  m
        request.addHeader("REST-API-Version", MBER_VERSION);
        HttpResponse response = client.execute(request);
        String body = toString(response.getEntity().getContent());
        return new Call(request.getMethod(), request.getURI(), response.getStatusLine().getStatusCode(), body);
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:ch.cyberduck.core.sds.provider.HttpComponentsConnector.java

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());
    }/*from w w  w .j  av a2 s .  c  o  m*/
    return stringHeaders;
}

From source file:com.android.internal.location.GpsXtraDownloader.java

protected static byte[] doDownload(String url, boolean isProxySet, String proxyHost, int proxyPort) {
    AndroidHttpClient client = null;/*  www  .  j  a v a2 s . c o  m*/
    try {
        client = AndroidHttpClient.newInstance("Android");
        HttpUriRequest req = new HttpGet(url);

        if (isProxySet) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            ConnRouteParams.setDefaultProxy(req.getParams(), proxy);
        }

        req.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");

        req.addHeader("x-wap-profile",
                "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#");

        HttpResponse response = client.execute(req);
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != 200) { // HTTP 200 is success.
            Log.d(TAG, "HTTP error: " + status.getReasonPhrase());
            return null;
        }

        HttpEntity entity = response.getEntity();
        byte[] body = null;
        if (entity != null) {
            try {
                if (entity.getContentLength() > 0) {
                    body = new byte[(int) entity.getContentLength()];
                    DataInputStream dis = new DataInputStream(entity.getContent());
                    try {
                        dis.readFully(body);
                    } finally {
                        try {
                            dis.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Unexpected IOException.", e);
                        }
                    }
                }
            } finally {
                if (entity != null) {
                    entity.consumeContent();
                }
            }
        }
        return body;
    } catch (Exception e) {
        Log.d(TAG, "error " + e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}

From source file:com.android.server.location.GpsXtraDownloader.java

protected static byte[] doDownload(String url, boolean isProxySet, String proxyHost, int proxyPort) {
    if (DEBUG)/*w w w . jav  a 2 s  . c o  m*/
        Log.d(TAG, "Downloading XTRA data from " + url);

    AndroidHttpClient client = null;
    try {
        client = AndroidHttpClient.newInstance("Android");
        HttpUriRequest req = new HttpGet(url);

        if (isProxySet) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            ConnRouteParams.setDefaultProxy(req.getParams(), proxy);
        }

        req.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");

        req.addHeader("x-wap-profile",
                "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#");

        HttpResponse response = client.execute(req);
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != 200) { // HTTP 200 is success.
            if (DEBUG)
                Log.d(TAG, "HTTP error: " + status.getReasonPhrase());
            return null;
        }

        HttpEntity entity = response.getEntity();
        byte[] body = null;
        if (entity != null) {
            try {
                if (entity.getContentLength() > 0) {
                    body = new byte[(int) entity.getContentLength()];
                    DataInputStream dis = new DataInputStream(entity.getContent());
                    try {
                        dis.readFully(body);
                    } finally {
                        try {
                            dis.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Unexpected IOException.", e);
                        }
                    }
                }
            } finally {
                if (entity != null) {
                    entity.consumeContent();
                }
            }
        }
        return body;
    } catch (Exception e) {
        if (DEBUG)
            Log.d(TAG, "error " + e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}