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

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

Introduction

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

Prototype

public static void setContentCharset(HttpParams httpParams, String str) 

Source Link

Usage

From source file:au.com.wallaceit.reddinator.RedditData.java

private boolean createHttpClient() {
    // Initialize client
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpConnectionParams.setConnectionTimeout(params, 12000);
    HttpConnectionParams.setSoTimeout(params, 10000);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    //cookieStore = new BasicCookieStore();
    httpclient = new DefaultHttpClient(conMgr, params);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
    //httpclient.setCookieStore(cookieStore);
    return true;/*  w w  w  . j  av a 2 s. co  m*/
}

From source file:code.google.restclient.core.Hitter.java

private HttpClient getHttpClient()
        throws KeyManagementException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException {
    // Early exit: already configured. Want all the gets to be fast so not synchronizing it
    if (client != null)
        return client;

    synchronized (lock) {
        HttpParams params = new SyncBasicHttpParams();

        // Set proxy
        if (isProxyConfigured()) {
            if (DEBUG_ENABLED)
                LOG.debug("CONFIGURING PROXY TO => " + proxyHost + ":" + proxyPort);
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            ConnRouteParams.setDefaultProxy(params, proxy);
        }/*from  w  ww . j a  v a  2 s  .  co  m*/

        // http params apply at client level so shared by all request. They can be overridden by params set at request level.
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, RCConstants.DEFAULT_CHARSET);
        HttpProtocolParams.setUserAgent(params, RCConstants.APP_DISPLAY_NAME);
        HttpClientParams.setRedirecting(params, true);

        /*
         HttpConnectionParams.setTcpNoDelay(params, true);
         HttpConnectionParams.setLinger(params, 30);
         HttpConnectionParams.setConnectionTimeout(params, 30000);
         HttpConnectionParams.setSoTimeout(params, 30000);
         HttpProtocolParams.setUseExpectContinue(params, false);
         HttpClientParams.setCookiePolicy(params,
         CookiePolicy.BROWSER_COMPATIBILITY);
         */

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(getPlainScheme());
        registry.register(getSSLScheme(isDisabledCertVerifier(), isDisabledHostVerifier()));

        if (conman == null)
            conman = new ThreadSafeClientConnManager(registry);

        if (client == null) {
            client = new DefaultHttpClient(conman);
            ((DefaultHttpClient) client).setParams(params);
            ((DefaultHttpClient) client).setKeepAliveStrategy(new CustomKeepAliveStrategy());
            /*
            // set custom request retry handler which doesn't allow retry for POST request
            HttpRequestRetryHandler retryHandler = new CustomRetryHandler();
            ((DefaultHttpClient) client).setHttpRequestRetryHandler(retryHandler);
            client.getParams().setParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 10000); // 10 seconds
            */

        }

        return client;
    }
}

From source file:srl.distributed.client.Client.java

private DefaultHttpClient getNewHttpClient() {
    try {//from   w  w  w  .  j av  a2s.  c om
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new InsecureSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:com.yibu.kuaibu.app.glApplication.java

private HttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
    HttpConnectionParams.setSoTimeout(params, 20 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(connMgr, params);
}

From source file:com.num.mobiperf.Checkin.java

/**
 * Return an appropriately-configured HTTP client.
 *//*from   w  ww. j ava  2 s  .  c  o  m*/
private HttpClient getNewHttpClient() {
    DefaultHttpClient client;
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        HttpConnectionParams.setConnectionTimeout(params, POST_TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(params, POST_TIMEOUT_MILLISEC);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        client = new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        // Logger.w("Unable to create SSL HTTP client", e);
        client = new DefaultHttpClient();
    }

    // TODO(mdw): For some reason this is not sending the cookie to the
    // test server, probably because the cookie itself is not properly
    // initialized. Below I manually set the Cookie header instead.
    CookieStore store = new BasicCookieStore();
    store.addCookie(authCookie);
    client.setCookieStore(store);
    return client;
}

From source file:com.imaginary.home.device.hue.HueMethod.java

protected @Nonnull HttpClient getClient() {
    boolean ssl = hue.getAPIEndpoint().startsWith("https");
    HttpParams params = new BasicHttpParams();

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    //noinspection deprecation
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    HttpProtocolParams.setUserAgent(params, "Imaginary Home");

    Properties p = hue.getCustomProperties();
    String proxyHost = p.getProperty("proxyHost");
    String proxyPort = p.getProperty("proxyPort");

    if (proxyHost != null) {
        int port = 0;

        if (proxyPort != null && proxyPort.length() > 0) {
            port = Integer.parseInt(proxyPort);
        }/*from   www.j  a v  a 2 s .c  om*/
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY,
                new HttpHost(proxyHost, port, ssl ? "https" : "http"));
    }
    return new DefaultHttpClient(params);
}

From source file:com.lgallardo.youtorrentcontroller.JSONParser.java

public JSONArray getJSONArrayFromUrl(String url) throws JSONParserStatusCodeException {

    // if server is published in a subfolder, fix url
    if (subfolder != null && !subfolder.equals("")) {
        url = subfolder + "/" + url;
    }/*from   w ww  .ja v a2 s. c om*/

    HttpResponse httpResponse;
    DefaultHttpClient httpclient;

    HttpParams httpParameters = new BasicHttpParams();

    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = connection_timeout * 1000;

    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = data_timeout * 1000;

    // Set http parameters
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpProtocolParams.setUserAgent(httpParameters, "youTorrent");
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);

    // Making HTTP request
    HttpHost targetHost = new HttpHost(hostname, port, protocol);

    httpclient = getNewHttpClient();

    // Set http parameters
    httpclient.setParams(httpParameters);

    try {

        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

        httpclient.getCredentialsProvider().setCredentials(authScope, credentials);

        url = protocol + "://" + hostname + ":" + port + "/" + url;

        HttpGet httpget = new HttpGet(url);

        if (this.cookie != null) {
            httpget.setHeader("Cookie", this.cookie);
        }

        httpResponse = httpclient.execute(targetHost, httpget);

        StatusLine statusLine = httpResponse.getStatusLine();

        int mStatusCode = statusLine.getStatusCode();

        if (mStatusCode != 200) {
            httpclient.getConnectionManager().shutdown();
            throw new JSONParserStatusCodeException(mStatusCode);
        }

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

        // Build JSON

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();

        jArray = new JSONArray(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    } catch (UnsupportedEncodingException e) {
    } catch (ClientProtocolException e) {
        Log.e("JSON", "Client: " + e.toString());
        e.printStackTrace();
    } catch (IOException e) {
        Log.e("JSON", "IO: " + e.toString());
        // e.printStackTrace();
        httpclient.getConnectionManager().shutdown();
        throw new JSONParserStatusCodeException(TIMEOUT_ERROR);
    } catch (JSONParserStatusCodeException e) {
        httpclient.getConnectionManager().shutdown();
        throw new JSONParserStatusCodeException(e.getCode());
    } catch (Exception e) {
        Log.e("JSON", "Generic: " + e.toString());
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources

        httpclient.getConnectionManager().shutdown();
    }

    // return JSON String
    return jArray;
}

From source file:de.geomobile.joined.api.service.JOWebService.java

/**
 * @return/*w ww  .  ja va2s  . com*/
 */
private HttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new JOSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 10000);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:com.lgallardo.qbittorrentclient.JSONParser.java

public JSONArray getJSONArrayFromUrl(String url) throws JSONParserStatusCodeException {

    // if server is published in a subfolder, fix url
    if (subfolder != null && !subfolder.equals("")) {
        url = subfolder + "/" + url;
    }/*from   www  .  j av a 2  s  .  c  o  m*/

    HttpResponse httpResponse;
    DefaultHttpClient httpclient;

    HttpParams httpParameters = new BasicHttpParams();

    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = connection_timeout * 1000;

    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = data_timeout * 1000;

    // Set http parameters
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpProtocolParams.setUserAgent(httpParameters, "qBittorrent for Android");
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);

    // Making HTTP request
    HttpHost targetHost = new HttpHost(hostname, port, protocol);

    httpclient = getNewHttpClient();

    httpclient.setParams(httpParameters);

    try {

        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

        httpclient.getCredentialsProvider().setCredentials(authScope, credentials);

        url = protocol + "://" + hostname + ":" + port + "/" + url;

        HttpGet httpget = new HttpGet(url);

        if (this.cookie != null) {
            httpget.setHeader("Cookie", this.cookie);
        }

        httpResponse = httpclient.execute(targetHost, httpget);

        StatusLine statusLine = httpResponse.getStatusLine();

        int mStatusCode = statusLine.getStatusCode();

        if (mStatusCode != 200) {
            httpclient.getConnectionManager().shutdown();
            throw new JSONParserStatusCodeException(mStatusCode);
        }

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

        // Build JSON

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();

        jArray = new JSONArray(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    } catch (UnsupportedEncodingException e) {
    } catch (ClientProtocolException e) {
        Log.e("JSON", "Client: " + e.toString());
        e.printStackTrace();
    } catch (SSLPeerUnverifiedException e) {
        Log.e("JSON", "SSLPeerUnverifiedException: " + e.toString());
        throw new JSONParserStatusCodeException(NO_PEER_CERTIFICATE);
    } catch (IOException e) {
        Log.e("JSON", "IO: " + e.toString());
        // e.printStackTrace();
        throw new JSONParserStatusCodeException(TIMEOUT_ERROR);
    } catch (JSONParserStatusCodeException e) {
        throw new JSONParserStatusCodeException(e.getCode());
    } catch (Exception e) {
        Log.e("JSON", "Generic: " + e.toString());
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources

        httpclient.getConnectionManager().shutdown();
    }

    // return JSON String
    return jArray;
}