Example usage for org.apache.http.params HttpProtocolParams setUserAgent

List of usage examples for org.apache.http.params HttpProtocolParams setUserAgent

Introduction

In this page you can find the example usage for org.apache.http.params HttpProtocolParams setUserAgent.

Prototype

public static void setUserAgent(HttpParams httpParams, String str) 

Source Link

Usage

From source file:com.mnxfst.testing.activities.http.TestHTTPRequestActivity.java

public void testExecuteHTTPRequest() throws HttpException, IOException {

    HttpParams params = new SyncBasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
    HttpProtocolParams.setUseExpectContinue(params, false);

    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
    HttpContext context = new BasicHttpContext(null);
    HttpHost host = new HttpHost("www.heise.de", 80);

    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

    try {/*from  w  w w  .  ja va2s  . c o m*/

        HttpEntity[] requestBodies = { new StringEntity("This is the first test request", "UTF-8"),
                new ByteArrayEntity("This is the second test request".getBytes("UTF-8")),
                new InputStreamEntity(new ByteArrayInputStream(
                        "This is the third test request (will be chunked)".getBytes("UTF-8")), -1) };

        HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
                // Required protocol interceptors
                new RequestContent(), new RequestTargetHost(),
                // Recommended protocol interceptors
                new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() });

        for (int i = 0; i < requestBodies.length; i++) {
            if (!conn.isOpen()) {
                Socket socket = new Socket(host.getHostName(), host.getPort());
                conn.bind(socket, params);
            }
            BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
            request.setEntity(requestBodies[i]);
            System.out.println(">> Request URI: " + request.getRequestLine().getUri());

            request.setParams(params);
            httpexecutor.preProcess(request, httpproc, context);
            HttpResponse response = httpexecutor.execute(request, conn, context);
            response.setParams(params);
            httpexecutor.postProcess(response, httpproc, context);

            System.out.println("<< Response: " + response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("==============");
            if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            } else {
                System.out.println("Connection kept alive...");
            }
        }
    } finally {
        conn.close();
    }

}

From source file:org.dasein.cloud.terremark.TerremarkMethod.java

public Document invoke(boolean debug) throws TerremarkException, CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("ENTER - " + TerremarkMethod.class.getName() + ".invoke(" + debug + ")");
    }// www  . ja  v  a  2 s .  com
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Talking to server at " + url);
        }

        if (parameters != null) {
            URIBuilder uri = null;
            try {
                uri = new URIBuilder(url);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            for (NameValuePair parameter : parameters) {
                uri.addParameter(parameter.getName(), parameter.getValue());
            }
            url = uri.toString();
        }

        HttpUriRequest method = null;
        if (methodType.equals(HttpMethodName.GET)) {
            method = new HttpGet(url);
        } else if (methodType.equals(HttpMethodName.POST)) {
            method = new HttpPost(url);
        } else if (methodType.equals(HttpMethodName.DELETE)) {
            method = new HttpDelete(url);
        } else if (methodType.equals(HttpMethodName.PUT)) {
            method = new HttpPut(url);
        } else if (methodType.equals(HttpMethodName.HEAD)) {
            method = new HttpHead(url);
        } else {
            method = new HttpGet(url);
        }
        HttpResponse status = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpParams params = new BasicHttpParams();

            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, "UTF-8");
            HttpProtocolParams.setUserAgent(params, "Dasein Cloud");

            attempts++;

            String proxyHost = provider.getProxyHost();
            if (proxyHost != null) {
                int proxyPort = provider.getProxyPort();
                boolean ssl = url.startsWith("https");
                params.setParameter(ConnRoutePNames.DEFAULT_PROXY,
                        new HttpHost(proxyHost, proxyPort, ssl ? "https" : "http"));
            }
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                method.addHeader(entry.getKey(), entry.getValue());
            }
            if (body != null && body != ""
                    && (methodType.equals(HttpMethodName.PUT) || methodType.equals(HttpMethodName.POST))) {
                try {
                    HttpEntity entity = new StringEntity(body, "UTF-8");
                    ((HttpEntityEnclosingRequestBase) method).setEntity(entity);
                } catch (UnsupportedEncodingException e) {
                    logger.warn(e);
                }
            }
            if (wire.isDebugEnabled()) {

                wire.debug(methodType.name() + " " + method.getURI());
                for (Header header : method.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                if (body != null) {
                    wire.debug(body);
                }
            }
            try {
                status = client.execute(method);
                if (wire.isDebugEnabled()) {
                    wire.debug("HTTP STATUS: " + status);
                }
            } catch (IOException e) {
                logger.error("I/O error from server communications: " + e.getMessage());
                e.printStackTrace();
                throw new InternalException(e);
            }
            int statusCode = status.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED
                    || statusCode == HttpStatus.SC_ACCEPTED) {
                try {
                    InputStream input = status.getEntity().getContent();

                    try {
                        return parseResponse(input);
                    } finally {
                        input.close();
                    }
                } catch (IOException e) {
                    logger.error("Error parsing response from Teremark: " + e.getMessage());
                    e.printStackTrace();
                    throw new CloudException(CloudErrorType.COMMUNICATION, statusCode, null, e.getMessage());
                }
            } else if (statusCode == HttpStatus.SC_NO_CONTENT) {
                logger.debug("Recieved no content in response. Creating an empty doc.");
                DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = null;
                try {
                    docBuilder = dbfac.newDocumentBuilder();
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                }
                return docBuilder.newDocument();
            } else if (statusCode == HttpStatus.SC_FORBIDDEN) {
                String msg = "OperationNotAllowed ";
                try {
                    msg += parseResponseToString(status.getEntity().getContent());
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                wire.error(msg);
                throw new TerremarkException(statusCode, "OperationNotAllowed", msg);
            } else {
                String response = "Failed to parse response.";
                ParsedError parsedError = null;
                try {
                    response = parseResponseToString(status.getEntity().getContent());
                    parsedError = parseErrorResponse(response);
                } catch (IllegalStateException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Received " + status + " from " + url);
                }
                if (statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE
                        || statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                    if (attempts >= 5) {
                        String msg;
                        wire.warn(response);
                        if (statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE) {
                            msg = "Cloud service is currently unavailable.";
                        } else {
                            msg = "The cloud service encountered a server error while processing your request.";
                            try {
                                msg = msg + "Response from server was:\n" + response;
                            } catch (RuntimeException runException) {
                                logger.warn(runException);
                            } catch (Error error) {
                                logger.warn(error);
                            }
                        }
                        wire.error(response);
                        logger.error(msg);
                        if (parsedError != null) {
                            throw new TerremarkException(parsedError);
                        } else {
                            throw new CloudException("HTTP Status " + statusCode + msg);
                        }
                    } else {
                        try {
                            Thread.sleep(5000L);
                        } catch (InterruptedException e) {
                            /* ignore */ }
                        return invoke();
                    }
                }
                wire.error(response);
                if (parsedError != null) {
                    throw new TerremarkException(parsedError);
                } else {
                    String msg = "\nResponse from server was:\n" + response;
                    logger.error(msg);
                    throw new CloudException("HTTP Status " + statusCode + msg);
                }
            }
        } finally {
            try {
                if (status != null) {
                    EntityUtils.consume(status.getEntity());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("EXIT - " + TerremarkMethod.class.getName() + ".invoke()");
        }
    }
}

From source file:com.twitter.hbc.ClientBuilder.java

public BasicClient build() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(params, USER_AGENT);
    HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
    return new BasicClient(name, hosts, endpoint, auth, enableGZip, processor, reconnectionManager, rateTracker,
            executorService, eventQueue, params, schemeRegistry);
}

From source file:org.eclipse.mylyn.commons.http.HttpUtil.java

public static void configureHttpClient(AbstractHttpClient client, String userAgent) {
    client.getParams().setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME,
            SingleConnectionManagerFactory.class.getName());

    client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    HttpProtocolParams.setUserAgent(client.getParams(), getUserAgent(userAgent));

    //      client.getParams().setLongParameter(AllClientPNames.CONNECTION_TIMEOUT, CONNNECT_TIMEOUT_INTERVAL);

    // TODO consider setting this as the default
    //client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    configureHttpClientConnectionManager(client);
}

From source file:com.hippoapp.asyncmvp.http.AsyncHttpClient.java

/**
 * Creates a new AsyncHttpClient and configure it with default parameters.
 *///from  w  w  w  .  j a  v  a  2 s .  com
public AsyncHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, socketTimeout);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections));
    ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);

    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(httpParams, String.format("su.bnet.applications", VERSION));

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);

    httpClient.setHttpRequestRetryHandler(new RetryHandler());

    threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();

    requestMap = new WeakHashMap<Context, List<WeakReference<Future>>>();
}

From source file:org.apache.shindig.gadgets.http.BasicHttpFetcher.java

/**
 * Creates a new fetcher for fetching HTTP objects.  Not really suitable
 * for production use. Use of an HTTP proxy for security is also necessary
 * for production deployment./*from w  ww . j av a2 s .  com*/
 *
 * @param maxObjSize          Maximum size, in bytes, of the object we will fetch, 0 if no limit..
 * @param connectionTimeoutMs timeout, in milliseconds, for connecting to hosts.
 * @param readTimeoutMs       timeout, in millseconds, for unresponsive connections
 * @param basicHttpFetcherProxy The http proxy to use.
 */
public BasicHttpFetcher(int maxObjSize, int connectionTimeoutMs, int readTimeoutMs,
        String basicHttpFetcherProxy) {
    // Create and initialize HTTP parameters
    setMaxObjectSizeBytes(maxObjSize);
    setSlowResponseWarning(DEFAULT_SLOW_RESPONSE_WARNING);

    HttpParams params = new BasicHttpParams();

    ConnManagerParams.setTimeout(params, connectionTimeoutMs);

    // These are probably overkill for most sites.
    ConnManagerParams.setMaxTotalConnections(params, 1152);
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(256));

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(params, "Apache Shindig");
    HttpProtocolParams.setContentCharset(params, "UTF-8");

    HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMs);
    HttpConnectionParams.setSoTimeout(params, readTimeoutMs);
    HttpConnectionParams.setStaleCheckingEnabled(params, true);

    HttpClientParams.setRedirecting(params, true);
    HttpClientParams.setAuthenticating(params, false);

    // Create and initialize scheme registry
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    DefaultHttpClient client = new DefaultHttpClient(cm, params);

    // Set proxy if set via guice.
    if (!StringUtils.isEmpty(basicHttpFetcherProxy)) {
        String[] splits = basicHttpFetcherProxy.split(":");
        ConnRouteParams.setDefaultProxy(client.getParams(),
                new HttpHost(splits[0], Integer.parseInt(splits[1]), "http"));
    }

    // try resending the request once
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(1, true));

    // Add hooks for gzip/deflate
    client.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final org.apache.http.HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip, deflate");
            }
        }
    });
    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final org.apache.http.HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    for (HeaderElement codec : ceheader.getElements()) {
                        String codecname = codec.getName();
                        if ("gzip".equalsIgnoreCase(codecname)) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            return;
                        } else if ("deflate".equals(codecname)) {
                            response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        }
    });
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler());

    // Disable automatic storage and sending of cookies (see SHINDIG-1382)
    client.removeRequestInterceptorByClass(RequestAddCookies.class);
    client.removeResponseInterceptorByClass(ResponseProcessCookies.class);

    // Use Java's built-in proxy logic in case no proxy set via guice.
    if (StringUtils.isEmpty(basicHttpFetcherProxy)) {
        ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
                client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
        client.setRoutePlanner(routePlanner);
    }

    FETCHER = client;
}

From source file:com.amazonaws.util.HttpUtils.java

/**
 * Fetches a file from the URI given and returns an input stream to it.
 *
 * @param uri the uri of the file to fetch
 * @param config optional configuration overrides
 * @return an InputStream containing the retrieved data
 * @throws IOException on error/*from   www  .j  a v  a  2s . c  o m*/
 */
public static InputStream fetchFile(final URI uri, final ClientConfiguration config) throws IOException {

    HttpParams httpClientParams = new BasicHttpParams();
    HttpProtocolParams.setUserAgent(httpClientParams, getUserAgent(config));

    HttpConnectionParams.setConnectionTimeout(httpClientParams, getConnectionTimeout(config));
    HttpConnectionParams.setSoTimeout(httpClientParams, getSocketTimeout(config));

    DefaultHttpClient httpclient = new DefaultHttpClient(httpClientParams);

    if (config != null) {
        String proxyHost = config.getProxyHost();
        int proxyPort = config.getProxyPort();

        if (proxyHost != null && proxyPort > 0) {

            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

            if (config.getProxyUsername() != null && config.getProxyPassword() != null) {

                httpclient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
                        new NTCredentials(config.getProxyUsername(), config.getProxyPassword(),
                                config.getProxyWorkstation(), config.getProxyDomain()));
            }
        }
    }

    HttpResponse response = httpclient.execute(new HttpGet(uri));

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new IOException("Error fetching file from " + uri + ": " + response);
    }

    return new HttpClientWrappingInputStream(httpclient, response.getEntity().getContent());
}

From source file:com.geekcap.javaworld.sparkexample.proxy.CustomClientBuilder.java

public BasicClient build() {
    HttpParams params = new BasicHttpParams();
    if (proxyHost != null) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }/* w  ww .  ja v a  2 s.c o  m*/

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(params, USER_AGENT);
    HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
    return new BasicClient(name, hosts, endpoint, auth, enableGZip, processor, reconnectionManager, rateTracker,
            executorService, eventQueue, params, schemeRegistry);
}

From source file:org.dasein.cloud.digitalocean.DigitalOcean.java

public @Nonnull HttpClient getClient(boolean multipart) throws InternalException {
    ProviderContext ctx = getContext();/*from  ww  w  . j  a  va 2 s . co m*/
    if (ctx == null) {
        throw new InternalException("No context was specified for this request");
    }

    final HttpParams params = new BasicHttpParams();
    int timeout = 15000;
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    if (!multipart) {
        HttpProtocolParams.setContentCharset(params, Consts.UTF_8.toString());
    }
    HttpProtocolParams.setUserAgent(params, "Dasein Cloud");

    Properties p = ctx.getCustomProperties();
    if (p != null) {
        String proxyHost = p.getProperty("proxyHost");
        String proxyPortStr = p.getProperty("proxyPort");
        int proxyPort = 0;
        if (proxyPortStr != null) {
            proxyPort = Integer.parseInt(proxyPortStr);
        }
        if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort));
        }
    }
    DefaultHttpClient client = new DefaultHttpClient(params);
    client.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
            request.setParams(params);
        }
    });
    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header header = entity.getContentEncoding();
                if (header != null) {
                    for (HeaderElement codec : header.getElements()) {
                        if (codec.getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            break;
                        }
                    }
                }
            }
        }
    });
    return client;
}