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.seajas.search.contender.http.ParameterizableHttpClient.java

/**
 * Default constructor./*  w w  w  .j  a  v a 2  s .  co m*/
 * 
 * @param connectionManager
 * @param parameters
 * @param httpHost
 * @param httpPort
 * @param userAgent
 * @param connectionTimeout
 */
public ParameterizableHttpClient(final ClientConnectionManager connectionManager, final HttpParams parameters,
        final String httpHost, final Integer httpPort, final String userAgent,
        final Integer connectionTimeout) {
    super(connectionManager, parameters);

    if (!StringUtils.isEmpty(httpHost))
        ConnRouteParams.setDefaultProxy(getParams(), new HttpHost(httpHost, httpPort));
    HttpProtocolParams.setUserAgent(getParams(), userAgent);

    if (connectionTimeout > 0) {
        HttpConnectionParams.setSoTimeout(getParams(), connectionTimeout);
        HttpConnectionParams.setConnectionTimeout(getParams(), connectionTimeout);
        HttpClientParams.setConnectionManagerTimeout(getParams(), connectionTimeout);
    }

    setRedirectStrategy(new DefaultRedirectStrategy());
}

From source file:com.blork.anpod.util.SyncUtils.java

/**
 * Generate and return a {@link HttpClient} configured for general use,
 * including setting an application-specific user-agent string.
 *
 * @param context the context/*from   w  w w .j  a v a 2s. c  om*/
 * @return the http client
 */
static HttpClient getHttpClient(Context context) {
    final HttpParams params = new BasicHttpParams();

    // Use generous timeouts for slow mobile networks
    HttpConnectionParams.setConnectionTimeout(params, 20 * SECOND_IN_MILLIS);
    HttpConnectionParams.setSoTimeout(params, 20 * SECOND_IN_MILLIS);

    HttpConnectionParams.setSocketBufferSize(params, 8192);
    HttpProtocolParams.setUserAgent(params, buildUserAgent(context));

    final DefaultHttpClient client = new DefaultHttpClient(params);

    client.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(HttpRequest request, HttpContext context) {
            // Add header to accept gzip content
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }
        }
    });

    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(HttpResponse response, HttpContext context) {
            // Inflate any responses compressed with gzip
            final HttpEntity entity = response.getEntity();
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    return client;
}

From source file:com.makotosan.vimeodroid.common.HttpHelper.java

/**
* Generate and return a {@link HttpClient} configured for general use,
* including setting an application-specific user-agent string.
*///w  w  w.  j  a  va2 s.c o m
public static HttpClient getHttpClient(Context context) {
    final HttpParams params = new BasicHttpParams();

    // Use generous timeouts for slow mobile networks
    HttpConnectionParams.setConnectionTimeout(params, 20 * SECOND_IN_MILLIS);
    HttpConnectionParams.setSoTimeout(params, 20 * SECOND_IN_MILLIS);

    HttpConnectionParams.setSocketBufferSize(params, 8192);
    HttpProtocolParams.setUserAgent(params, buildUserAgent(context));

    final DefaultHttpClient client = new DefaultHttpClient(params);

    client.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override
        public void process(HttpRequest request, HttpContext context) {
            // Add header to accept gzip content
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }
        }
    });

    client.addResponseInterceptor(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) {
            // Inflate any responses compressed with gzip
            final HttpEntity entity = response.getEntity();
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    return client;
}

From source file:mpv5.utils.http.HttpClient.java

/**
 * Connects to the given host/*w w w  .  j  a  v  a 2  s  .  co  m*/
 * @param toHost
 * @param port
 * @throws UnknownHostException
 * @throws IOException
 * @throws HttpException
 */
public HttpClient(String toHost, int port) throws UnknownHostException, IOException, HttpException {
    params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
    HttpProtocolParams.setUseExpectContinue(params, true);

    httpproc = new BasicHttpProcessor();
    // Required protocol interceptors
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    // Recommended protocol interceptors
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());
    httpexecutor = new HttpRequestExecutor();
    context = new BasicHttpContext(null);
    host = new HttpHost(toHost, port);
    conn = new DefaultHttpClientConnection();
    connStrategy = new DefaultConnectionReuseStrategy();
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
}

From source file:com.intel.cosbench.client.http.HttpClientUtil.java

private static HttpParams createDefaultHttpParams(int timeout) {
    HttpParams params = new BasicHttpParams();
    /* default HTTP parameters */
    DefaultHttpClient.setDefaultHttpParams(params);
    /* connection/socket timeouts */
    HttpConnectionParams.setSoTimeout(params, timeout);
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    /* user agent */
    HttpProtocolParams.setUserAgent(params, "cosbench/2.0");
    return params;
}

From source file:de.mendelson.comm.as2.client.HTMLPanel.java

/**Sets a new page to the viewer and handles the Stack update
 *@param url URL to move to//from  w  w  w.j a v a 2  s.  c  om
 */
public Header[] setURL(String urlStr, String userAgent, File fallbackOnError) {
    Header[] header = new Header[0];
    try {
        //post for header data
        HttpPost filePost = new HttpPost(new URL(urlStr).toExternalForm());
        HttpParams params = filePost.getParams();
        HttpProtocolParams.setUserAgent(params, userAgent);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(filePost);
        header = httpResponse.getAllHeaders();
        int status = httpResponse.getStatusLine().getStatusCode();
        if (status != HttpServletResponse.SC_OK) {
            throw new Exception("HTTP " + status);
        }
        httpClient.getConnectionManager().shutdown();
        //now get for body data
        this.jEditorPane.setPage(new URL(urlStr));
    } catch (Exception e) {
        e.printStackTrace();
        try {
            this.setPage(fallbackOnError);
        } catch (Exception ex) {
            //nop
        }
    }
    return (header);
}

From source file:net.bitquill.ocr.weocr.WeOCRClient.java

public WeOCRClient(String endpoint) {
    mEndpoint = endpoint;/*  ww w  .ja va 2  s.  c  om*/
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpProtocolParams.setUserAgent(params, USER_AGENT_STRING);
    HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
    mHttpClient = new DefaultHttpClient(params);
}

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

@Override
public synchronized void process(HttpRequest request) {
    if (userAgent != null) {
        if (userAgentOverride != false) {
            request.setHeader(new BasicHeader("User-Agent", userAgent));
        } else {//from   w  w  w .  j  a  v  a2 s  . c om
            HttpProtocolParams.setUserAgent(request.getParams(), userAgent);
        }
    }

    if (disableBrowserCache) {
        for (String header : CONDITIONAL_REQUEST_HEADERS) {
            request.removeHeaders(header);
        }
    }

    if (disableProxyCache) {
        // instruct HTTP/1.1 caches to do an end-to-end reload
        request.setHeader(new BasicHeader("Cache-Control", "no-cache"));

        // instruct HTTP/1.0 caches to (hopefully) do an end-to-end reload
        request.setHeader(new BasicHeader("Pragma", "no-cache"));
    }
}

From source file:com.entertailion.android.launcher.utils.HttpRequestHelper.java

public static DefaultHttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 60000);
    HttpConnectionParams.setSoTimeout(params, 60000);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUserAgent(params,
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.5 Safari/537.22");

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(conMgr, params);
}

From source file:com.dubsar_dictionary.SecureClient.SecureAndroidHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 * (Lifted and modified from AndroidHttpClient.)
 *
 * @param userAgent to report in your HTTP requests
 * @param context to use for caching SSL sessions (may be null for no caching)
 * @return AndroidHttpClient for you to use for all your requests.
 *///www .j  a  v  a2s  . c  om
public static HttpClient newInstance(String userAgent) {
    Log.d(TAG, "Creating new client instance");

    HttpParams params = new BasicHttpParams();

    // Turn off stale checking.  Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);

    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    // Default to following redirects
    HttpClientParams.setRedirecting(params, true);

    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    SSLSocketFactory sf = SecureSocketFactory.getHttpSocketFactory(SOCKET_OPERATION_TIMEOUT);
    schemeRegistry.register(new Scheme("https", sf, 443));
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

    // We use a factory method to modify superclass initialization
    // parameters without the funny call-a-static-method dance.
    return new SecureAndroidHttpClient(manager, params);
}