Example usage for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT

List of usage examples for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT.

Prototype

String USER_AGENT

To view the source code for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT.

Click Source Link

Usage

From source file:fr.unix_experience.owncloud_sms.engine.OCHttpClient.java

public OCHttpClient(Context context, Uri serverURI, String accountName, String accountPassword) {
    super(new MultiThreadedHttpConnectionManager());
    Protocol easyhttps = new Protocol("https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), 443);
    Protocol.registerProtocol("https", easyhttps);
    _serverURI = serverURI;//from w  w  w  . j a v a2  s  . c o m
    _username = accountName;
    _password = accountPassword;
    getParams().setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    getParams().setParameter(PARAM_PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    getParams().setParameter(HttpMethodParams.USER_AGENT,
            "nextcloud-phonesync (" + new AndroidVersionProvider(context).getVersionCode() + ")");
}

From source file:com.cerema.cloud2.lib.common.OwnCloudClient.java

/**
 * Constructor//from w ww.  j ava2 s  .  c  o m
 */
public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) {
    super(connectionMgr);

    if (baseUri == null) {
        throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
    }
    mBaseUri = baseUri;

    mInstanceNumber = sIntanceCounter++;
    Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient");

    String userAgent = OwnCloudClientManagerFactory.getUserAgent();
    getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
    getParams().setParameter(PARAM_PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    getParams().setParameter(PARAM_SINGLE_COOKIE_HEADER, // to avoid problems with some web servers
            PARAM_SINGLE_COOKIE_HEADER_VALUE);

    applyProxySettings();

    clearCredentials();
}

From source file:com.fatwire.dta.sscrawler.URLReaderService.java

protected HttpClient initClient() {
    final HttpClient client = new HttpClient(connectionManager);
    client.getHostConfiguration().setHost(hostConfig.getHostname(), hostConfig.getPort(),
            hostConfig.getProtocol());//from   w  w  w  . j  a va 2 s.  c  o m

    if (hostConfig.getProxyHost() != null) {
        client.getHostConfiguration().setProxyHost(hostConfig.getProxyHost());
        if (hostConfig.getProxyCredentials() != null) {
            client.getState().setProxyCredentials(AuthScope.ANY, hostConfig.getProxyCredentials());
        }
    }

    client.getParams().setParameter(HttpMethodParams.USER_AGENT, "ss-crawler-0.9");

    // RFC 2101 cookie management spec is used per default
    // to parse, validate, format & match cookies
    // client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    // client.getParams().setCookiePolicy(CookiePolicy.DEFAULT);
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    // client.getParams().makeStrict();
    client.getParams().getDefaults().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);

    client.getState().addCookie(new Cookie(hostConfig.getHostname(), HelperStrings.SS_CLIENT_INDICATOR,
            Boolean.TRUE.toString(), hostConfig.getDomain(), -1, false));
    return client;
}

From source file:ch.cyberduck.core.http.HTTP3Session.java

protected HostConfiguration getHostConfiguration(Host host) {
    int port = host.getPort();
    final HostConfiguration configuration = new StickyHostConfiguration();
    if ("https".equals(host.getProtocol().getScheme())) {
        if (-1 == port) {
            port = 443;/*from   w ww .j  a v a  2 s .c  o  m*/
        }
        // Configuration with custom socket factory using the trust manager
        configuration.setHost(host.getHostname(), port,
                new org.apache.commons.httpclient.protocol.Protocol(host.getProtocol().getScheme(),
                        new SSLSocketFactory(this.getTrustManager(host.getHostname())), port));
        if (Preferences.instance().getBoolean("connection.proxy.enable")) {
            final Proxy proxy = ProxyFactory.instance();
            if (proxy.isHTTPSProxyEnabled(host)) {
                configuration.setProxy(proxy.getHTTPSProxyHost(host), proxy.getHTTPSProxyPort(host));
            }
        }
    } else if ("http".equals(host.getProtocol().getScheme())) {
        if (-1 == port) {
            port = 80;
        }
        configuration.setHost(host.getHostname(), port, new org.apache.commons.httpclient.protocol.Protocol(
                host.getProtocol().getScheme(), new DefaultProtocolSocketFactory(), port));
        if (Preferences.instance().getBoolean("connection.proxy.enable")) {
            final Proxy proxy = ProxyFactory.instance();
            if (proxy.isHTTPProxyEnabled(host)) {
                configuration.setProxy(proxy.getHTTPProxyHost(host), proxy.getHTTPProxyPort(host));
            }
        }
    }
    final HostParams parameters = configuration.getParams();
    parameters.setParameter(HttpMethodParams.USER_AGENT, this.getUserAgent());
    parameters.setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    parameters.setParameter(HttpMethodParams.SO_TIMEOUT, this.timeout());
    parameters.setParameter(HttpMethodParams.CREDENTIAL_CHARSET, "ISO-8859-1");
    parameters.setParameter(HttpClientParams.MAX_REDIRECTS, 10);
    return configuration;
}

From source file:easyshop.downloadhelper.HttpPageGetter.java

public HttpPage getHttpPage(PageRef thisRef, HttpClient client, String charSet) {
    client.getParams().setParameter(HttpMethodParams.USER_AGENT, HTTP_USER_AGENT); //?IE
    String urlStr = thisRef.getUrlStr();
    //       try {
    //         urlStr=new String(urlStr.getBytes("utf-8"),"gb2312");
    //      } catch (UnsupportedEncodingException e1) {
    //         // log error here
    //         log.error(e1.getMessage());
    //      }//from   w w  w  . ja  v a2  s.co  m
    //       System.out.println(urlStr);
    //        get.setRequestHeader("connection","keep-alive");
    GetMethod get = null;

    try {

        get = new GetMethod(urlStr);
        get.setFollowRedirects(true); //????
        long startTime = System.currentTimeMillis();
        int iGetResultCode = client.executeMethod(get);
        Header[] rheaders = get.getRequestHeaders();
        Header[] headers = get.getResponseHeaders();
        boolean is11 = get.isHttp11();
        boolean redirect = get.getFollowRedirects();

        if (get.getResponseContentLength() >= 2024000) {
            log.info("content is too large, can't download!");
            ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
            return new OriHttpPage(-1, urlStr, null, null, conRes, null);
        }

        BufferedInputStream remoteBIS = new BufferedInputStream(get.getResponseBodyAsStream());
        ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
        byte[] buf = new byte[1024];
        int bytesRead = 0;
        while (bytesRead >= 0) {
            baos.write(buf, 0, bytesRead);
            bytesRead = remoteBIS.read(buf);
        }
        remoteBIS.close();
        byte[] content = baos.toByteArray();
        //            byte[] content=get.getResponseBody();

        long timeTaken = System.currentTimeMillis() - startTime;
        if (timeTaken < 100)
            timeTaken = 500;
        int bytesPerSec = (int) ((double) content.length / ((double) timeTaken / 1000.0));
        //            log.info("Downloaded " + content.length + " bytes, " + bytesPerSec + " bytes/sec");
        //            log.info("urlstr:"+urlStr);
        ConnResponse conRes = new ConnResponse(get.getResponseHeader("Content-type").getValue(), null, 0, 0,
                get.getStatusCode());
        String charset = conRes.getCharSet();
        if (charset == null) {
            String cc = new String(content);
            if (cc.indexOf("content=\"text/html; charset=gb2312") > 0)
                charset = "gb2312";
            else if (cc.indexOf("content=\"text/html; charset=utf-8") > 0)
                charset = "utf-8";
            else if (cc.indexOf("content=\"text/html; charset=gbk") > 0)
                charset = "gbk";
        }
        return new HttpPage(urlStr, content, conRes, charset);

    } catch (IOException ioe) {
        log.warn("Caught IO Exception: " + ioe.getMessage(), ioe);
        failureCount++;
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new OriHttpPage(-1, urlStr, null, null, conRes, null);
    } catch (Exception e) {
        log.warn("Caught Exception: " + e.getMessage(), e);
        failureCount++;
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new OriHttpPage(-1, urlStr, null, null, conRes, null);
    } finally {
        get.releaseConnection();
    } /**/
}

From source file:ac.elements.io.Signature.java

/**
 * Configure HttpClient with set of defaults as well as configuration from
 * AmazonEC2Config instance.//from  w ww  .  j a  va 2s . co m
 * 
 * @return the http client
 */
private static HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
    httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > MAX_RETRY_ERROR) {
                log.warn("Maximum Number of Retry attempts " + "reached, will not retry");
                return false;
            }
            log.warn("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.warn("Retrying on NoHttpResponseException");
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                log.warn("Will not retry on InterruptedIOException", exception);
                return false;
            }
            if (exception instanceof UnknownHostException) {
                log.warn("Will not retry on UnknownHostException", exception);
                return false;
            }
            if (!method.isRequestSent()) {
                log.warn("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(MAX_CONNECTIONS);
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, MAX_CONNECTIONS);

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    // if (config.isSetProxyHost() && config.isSetProxyPort()) {
    // log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() +
    // "Proxy Port: " + config.getProxyPort() );
    // hostConfiguration.setProxy(config.getProxyHost(),
    // config.getProxyPort());
    // if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
    // httpClient.getState().setProxyCredentials (new AuthScope(
    // config.getProxyHost(),
    // config.getProxyPort()),
    // new UsernamePasswordCredentials(
    // config.getProxyUsername(),
    // config.getProxyPassword()));
    //        
    // }
    // }
    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:com.cerema.cloud2.lib.common.OwnCloudClient.java

/**
 * Requests the received method./*from w w  w. j a  v a2 s.  c  o m*/
 *
 * Executes the method through the inherited HttpClient.executedMethod(method).
 *
 * @param method                HTTP method request.
 */
@Override
public int executeMethod(HttpMethod method) throws IOException {
    try {
        // Update User Agent
        HttpParams params = method.getParams();
        String userAgent = OwnCloudClientManagerFactory.getUserAgent();
        params.setParameter(HttpMethodParams.USER_AGENT, userAgent);

        Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath());

        //           logCookiesAtRequest(method.getRequestHeaders(), "before");
        //           logCookiesAtState("before");
        method.setFollowRedirects(false);

        int status = super.executeMethod(method);

        if (mFollowRedirects) {
            status = followRedirection(method).getLastStatus();
        }

        //           logCookiesAtRequest(method.getRequestHeaders(), "after");
        //           logCookiesAtState("after");
        //           logSetCookiesAtResponse(method.getResponseHeaders());

        return status;

    } catch (IOException e) {
        //Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e);
        throw e;
    }
}

From source file:ir.keloud.android.lib.common.KeloudClient.java

@Override
public int executeMethod(HttpMethod method) throws IOException, HttpException {
    try { // just to log 
        boolean customRedirectionNeeded = false;

        try {/*  w  w w. j a v  a2 s .co m*/
            method.setFollowRedirects(mFollowRedirects);
        } catch (Exception e) {
            /*
             if (mFollowRedirects) 
               Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName() 
            + " method, custom redirection will be used if needed");
            */
            customRedirectionNeeded = mFollowRedirects;
        }

        // Update User Agent
        HttpParams params = method.getParams();
        String userAgent = KeloudClientManagerFactory.getUserAgent();
        params.setParameter(HttpMethodParams.USER_AGENT, userAgent);

        Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath());

        //           logCookiesAtRequest(method.getRequestHeaders(), "before");
        //           logCookiesAtState("before");

        int status = super.executeMethod(method);

        if (customRedirectionNeeded) {
            status = patchRedirection(status, method);
        }

        //           logCookiesAtRequest(method.getRequestHeaders(), "after");
        //           logCookiesAtState("after");
        //           logSetCookiesAtResponse(method.getResponseHeaders());

        return status;

    } catch (IOException e) {
        Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e);
        throw e;
    }
}

From source file:com.amazonaws.elasticmapreduce.AmazonElasticMapReduceClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonElasticMapReduceConfig instance
 *
 *//*from   ww w . jav a2  s.  c o m*/
private HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, config.getUserAgent());
    httpClientParams.setParameter(HttpClientParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > 3) {
                log.debug("Maximum Number of Retry attempts reached, will not retry");
                return false;
            }
            log.debug("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.debug("Retrying on NoHttpResponseException");
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                log.debug("Will not retry on InterruptedIOException", exception);
                return false;
            }
            if (exception instanceof UnknownHostException) {
                log.debug("Will not retry on UnknownHostException", exception);
                return false;
            }
            if (!method.isRequestSent()) {
                log.debug("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(config.getMaxConnections());
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, config.getMaxConnections());

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        hostConfiguration.setProxy(config.getProxyHost(), config.getProxyPort());
        if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
            httpClient.getState().setProxyCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));

        }
    }

    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:com.zenkey.net.prowser.Tab.java

/**************************************************************************
 * Configures the HttpMethod object before use.
 * /*from   w w w .j a  va2s.co  m*/
 * @param httpMethod
 *        The HTTP method object.
 * @param request
 *        The request object.
 * @param ioTimeout
 *        The timeout value to use for I/O operations (like making
 *        connections and socket reads). This value should be 1 second
 *        longer than the actual timeout used for the request, in order to
 *        allow the request to timeout cleanly without having an I/O
 *        exception get in the way.
 * @param httpMethodRetryHandler
 *        The retry handler object.
 * @throws ProtocolException
 *         If the HTTP version in the request object is invalid.
 */
private static void prepareHttpMethod(HttpMethod httpMethod, Request request,
        HttpMethodRetryHandler httpMethodRetryHandler) throws ProtocolException {

    httpMethod.setFollowRedirects(false);

    httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, httpMethodRetryHandler);

    String httpVersion = null;
    if ((httpVersion = request.getHttpVersion()) != null)
        httpMethod.getParams().setVersion(HttpVersion.parse("HTTP/" + httpVersion));

    String userAgent = null;
    if ((userAgent = request.getUserAgent()) != null)
        httpMethod.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
}