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:luki.x.net.XNetEngine.java

/**
 * getHttpClient// w ww .  jav a2  s.c om
 * 
 * @return
 */
private HttpClient getHttpClient() {
    // ?
    // httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost("", 0));
    // httpParams.removeParameter(ConnRoutePNames.DEFAULT_PROXY);
    if (httpClient == null) {
        // timeout: get connections from connection pool
        ConnManagerParams.setTimeout(httpParams, 1000);
        // timeout: connect to the server
        HttpConnectionParams.setConnectionTimeout(httpParams, DEFAULT_SOCKET_TIMEOUT);
        // timeout: transfer data from server
        HttpConnectionParams.setSoTimeout(httpParams, DEFAULT_SOCKET_TIMEOUT);

        // set max connections per host
        ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(DEFAULT_HOST_CONNECTIONS));
        // set max total connections
        ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);

        // use expect-continue handshake
        HttpProtocolParams.setUseExpectContinue(httpParams, true);

        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(httpParams, charSet);
        // disable Nagle algorithm
        HttpConnectionParams.setTcpNoDelay(httpParams, true);

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

        ClientConnectionManager manager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
        httpClient = new DefaultHttpClient(manager, httpParams);
    }

    return httpClient;
}

From source file:net.paissad.waqtsalat.service.utils.HttpUtils.java

private static HttpClient getNewHttpClient() throws WSException {

    try {/*from www.  j av  a2 s  . c  o  m*/
        TrustStrategy trustStrategy = new TrustStrategy() {

            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        };
        SSLSocketFactory sf = new CustomSSLFactory(trustStrategy);

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

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

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(sr);

        DefaultHttpClient client = new DefaultHttpClient(ccm);
        client.setHttpRequestRetryHandler(new CustomHttpRequestRetryHandler());
        client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true);
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);

        List<String> authpref = new ArrayList<String>();
        // Choose BASIC over DIGEST for proxy authentication
        authpref.add(AuthPolicy.BASIC);
        authpref.add(AuthPolicy.DIGEST);
        client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);

        client.addRequestInterceptor(new CustomHttpRequestInterceptor());
        client.addResponseInterceptor(new CustomHttpResponseInterceptor());

        return client;

    } catch (Exception e) {
        throw new WSException("Error while creating a HTTP client.", e);
    }
}

From source file:com.catchnotes.api.CatchAPI.java

private HttpClient getHttpClientNoToken() {
    if (httpClientNoToken == null) {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setUserAgent(params, userAgent);
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        httpClientNoToken = new DefaultHttpClient(params);
    }//from   www .  j  a v a2 s . c o  m

    return httpClientNoToken;
}

From source file:com.zrlh.llkc.funciton.Http_Utility.java

public static HttpClient getNewHttpClient(Context context) {
    try {//  www  .  j a  v a2s. co m
        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);

        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);
        // Set the default socket timeout (SO_TIMEOUT) // in
        // milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setConnectionTimeout(params, Http_Utility.SET_CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, Http_Utility.SET_SOCKET_TIMEOUT);
        HttpClient client = new DefaultHttpClient(ccm, params);

        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifiManager.getConnectionInfo();
        if (!wifiManager.isWifiEnabled() || -1 == info.getNetworkId()) {
            // ??APN?
            Uri uri = Uri.parse("content://telephony/carriers/preferapn");
            Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                // ???
                String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
                if (proxyStr != null && proxyStr.trim().length() > 0) {
                    HttpHost proxy = new HttpHost(proxyStr, 80);
                    client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
                }
                mCursor.close();
            }
        }
        return client;
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:com.wiyun.engine.network.Network.java

static DefaultHttpClient createHttpClient(int timeout) {
    // wifi connected?
    boolean wifi = isWifiConnected();

    // create client
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    try {/*from ww  w  .j  a va2 s .  com*/
        schemeRegistry.register(new Scheme("https", new TrustAllSSLSocketFactory(), 443));
    } catch (Exception e) {
    }
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(params, false);
    HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);
    HttpConnectionParams.setConnectionTimeout(params, wifi ? timeout : (timeout * 3));
    HttpConnectionParams.setSoTimeout(params, wifi ? timeout : (timeout * 3));
    ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, schemeRegistry);
    DefaultHttpClient client = new DefaultHttpClient(ccm, params);

    if (!wifi && hasProxy()) {
        HttpHost proxy = getProxy();
        if (proxy != null)
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    return client;
}

From source file:com.prasanna.android.http.SecureHttpHelper.java

protected HttpClient createSecureHttpClient() {
    try {/*ww  w.ja v a2 s . co  m*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new SSLSocketFactoryX509(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 schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme(SCHEME_HTTPS, sf, HTTPS_PORT));
        schemeRegistry.register(new Scheme(SCHEME_HTTP, PlainSocketFactory.getSocketFactory(), HTTP_PORT));

        return new DefaultHttpClient(new SingleClientConnManager(params, schemeRegistry), params);
    } catch (KeyManagementException e) {
        LogWrapper.e(TAG, e.getMessage());
    } catch (UnrecoverableKeyException e) {
        LogWrapper.e(TAG, e.getMessage());
    } catch (KeyStoreException e) {
        LogWrapper.e(TAG, e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        LogWrapper.e(TAG, e.getMessage());
    } catch (CertificateException e) {
        LogWrapper.e(TAG, e.getMessage());
    } catch (IOException e) {
        LogWrapper.e(TAG, e.getMessage());
    }

    throw new ClientException(ClientErrorCode.HTTP_REQ_ERROR);
}

From source file:com.payu.sdk.helper.HttpClientHelper.java

/**
 * Sets the parameters to the request/*w  w w . ja va  2  s  .  c  o  m*/
 *
 * @param params
 *            The parameters to be set
 * @param the socket time out.
 */
private static void setHttpClientParameters(HttpParams params, Integer socketTimeOut) {

    HttpProtocolParams.setContentCharset(params, Constants.DEFAULT_ENCODING);
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, socketTimeOut);
}

From source file:com.allblacks.utils.web.HttpUtil.java

static HttpClient getNewHttpClient() {
    try {//from  w w  w  .j a  v  a  2 s .c o  m
        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);

        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:fr.univsavoie.ltp.client.LoginActivity.java

/**
 * Pav de code permetant de se connecter de faon scuris au serveur
 *//*from  w w w .j a  va2 s .  c o m*/
private void auth() {
    try {
        HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
            public void process(final HttpRequest request, final HttpContext context)
                    throws HttpException, IOException {
                AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
                CredentialsProvider credsProvider = (CredentialsProvider) context
                        .getAttribute(ClientContext.CREDS_PROVIDER);
                HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

                if (authState.getAuthScheme() == null) {
                    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    Credentials creds = credsProvider.getCredentials(authScope);
                    if (creds != null) {
                        authState.setAuthScheme(new BasicScheme());
                        authState.setCredentials(creds);
                    }
                }
            }
        };

        // Setup a custom SSL Factory object which simply ignore the certificates validation and accept all type of self signed certificates
        SSLSocketFactory sslFactory = new SimpleSSLSocketFactory(null);
        sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

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

        // Register the HTTP and HTTPS Protocols. For HTTPS, register our custom SSL Factory object.
        SchemeRegistry registry = new SchemeRegistry();
        // registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sslFactory, 443));

        // Create a new connection manager using the newly created registry and then create a new HTTP client using this connection manager
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        httpClient = new DefaultHttpClient(ccm, params);

        CredentialsProvider authCred = new BasicCredentialsProvider();
        Credentials creds = new UsernamePasswordCredentials(login.getText().toString(),
                password.getText().toString());
        authCred.setCredentials(AuthScope.ANY, creds);

        httpClient.addRequestInterceptor(preemptiveAuth, 0);
        httpClient.setCredentialsProvider(authCred);
    } catch (Exception e) {
        Log.e("Catch", "Auth: " + e.getLocalizedMessage());
    }
}

From source file:de.teambluebaer.patientix.helper.RestfulHelper.java

public HttpClient createHttpClient() {
    try {// ww w.j  av a  2  s  .  co  m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        MySSLSocketFactory 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);

        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();
    }
}