Example usage for org.apache.http.params CoreProtocolPNames HTTP_ELEMENT_CHARSET

List of usage examples for org.apache.http.params CoreProtocolPNames HTTP_ELEMENT_CHARSET

Introduction

In this page you can find the example usage for org.apache.http.params CoreProtocolPNames HTTP_ELEMENT_CHARSET.

Prototype

String HTTP_ELEMENT_CHARSET

To view the source code for org.apache.http.params CoreProtocolPNames HTTP_ELEMENT_CHARSET.

Click Source Link

Usage

From source file:edu.tum.cs.ias.knowrob.BarcodeWebLookup.java

public static String lookUpEANsearch(String ean) {

    String res = "";
    if (ean != null && ean.length() > 0) {

        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        //httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-16");

        try {/*  w  w w. j  a va  2  s  .  c om*/

            HttpGet httpget = new HttpGet("http://www.ean-search.org/perl/ean-search.pl?ean=" + ean + "&os=1");
            httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
            httpget.getParams().setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "ASCII");

            System.out.println(httpget.getURI());

            // Create a response handler

            ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
                public byte[] handleResponse(HttpResponse response)
                        throws ClientProtocolException, IOException {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        return EntityUtils.toByteArray(entity);
                    } else {
                        return null;
                    }
                }
            };

            byte[] response = httpclient.execute(httpget, handler);

            //   String responseBody = httpclient.execute(httpget, handler);

            //            new HeapByteBuffer(responseBody.getBytes(), 0, responseBody.getBytes().length));
            //            
            //            Charset a  = Charset.forName("UTF-8");
            //            a.newEncoder().encode(responseBody.getBytes());
            //            
            //            System.out.println(responseBody);

            // Parse response document
            res = response.toString();

        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        } catch (ClientProtocolException cpe) {
            cpe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (httpclient != null) {
                httpclient.getConnectionManager().shutdown();
            }
        }

    }
    return res;
}

From source file:com.norconex.collector.http.client.impl.DefaultHttpClientInitializer.java

@Override
public void initializeHTTPClient(DefaultHttpClient httpClient) {

    // Time out after 30 seconds.
    //TODO Make configurable.
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);

    // Add support for FTP websites (FTP served by HTTP server).
    Scheme ftp = new Scheme("ftp", FTP_PORT, new PlainSocketFactory());
    httpClient.getConnectionManager().getSchemeRegistry().register(ftp);

    //TODO make charset configurable instead since UTF-8 is not right
    // charset for URL specifications.  It is used here to overcome
    // so invalid redirect errors, where the redirect target URL is not 
    // URL-Encoded and has non-ascii values, and fails
    // (e.g. like ja.wikipedia.org).
    // Can consider a custom RedirectStrategy too if need be.
    httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "UTF-8");

    if (StringUtils.isNotBlank(proxyHost)) {
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort));
        if (StringUtils.isNotBlank(proxyUsername)) {
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
                    new UsernamePasswordCredentials(proxyUsername, proxyPassword));
        }//from  ww w.j a va  2s .com
    }

    if (!cookiesDisabled) {
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    }
    if (AUTH_METHOD_FORM.equalsIgnoreCase(authMethod)) {
        authenticateUsingForm(httpClient);
    } else if (AUTH_METHOD_BASIC.equalsIgnoreCase(authMethod)) {
        setupBasicDigestAuth(httpClient);
    } else if (AUTH_METHOD_DIGEST.equalsIgnoreCase(authMethod)) {
        setupBasicDigestAuth(httpClient);
    }
    if (userAgent != null) {
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
    }
}

From source file:com.sangupta.jerry.http.WebRequest.java

/**
* Set the {@link Charset} for the given request
* 
* @param charset//w  w w .j  av  a  2s.  com
*            the {@link Charset} to use
* 
* @return this very {@link WebRequest}
*/
public WebRequest elementCharset(final String charset) {
    return config(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, charset);
}

From source file:org.apache.axis2.transport.http.impl.httpclient4.HTTPSenderImpl.java

protected AbstractHttpClient getHttpClient(MessageContext msgContext) {
    ConfigurationContext configContext = msgContext.getConfigurationContext();

    AbstractHttpClient httpClient = (AbstractHttpClient) msgContext
            .getProperty(HTTPConstants.CACHED_HTTP_CLIENT);

    if (httpClient == null) {
        httpClient = (AbstractHttpClient) configContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
    }// ww w .j a v  a2  s  .  c  o  m

    if (httpClient != null) {
        return httpClient;
    }

    synchronized (this) {
        httpClient = (AbstractHttpClient) msgContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);

        if (httpClient == null) {
            httpClient = (AbstractHttpClient) configContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
        }

        if (httpClient != null) {
            return httpClient;
        }

        ClientConnectionManager connManager = (ClientConnectionManager) msgContext
                .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
        if (connManager == null) {
            connManager = (ClientConnectionManager) msgContext
                    .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
        }
        if (connManager == null) {
            // reuse HttpConnectionManager
            synchronized (configContext) {
                connManager = (ClientConnectionManager) configContext
                        .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
                if (connManager == null) {
                    log.trace("Making new ConnectionManager");
                    SchemeRegistry schemeRegistry = new SchemeRegistry();
                    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
                    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

                    connManager = new PoolingClientConnectionManager(schemeRegistry);
                    ((PoolingClientConnectionManager) connManager).setMaxTotal(200);
                    ((PoolingClientConnectionManager) connManager).setDefaultMaxPerRoute(200);
                    configContext.setProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER, connManager);
                }
            }
        }
        /*
         * Create a new instance of HttpClient since the way it is used here
         * it's not fully thread-safe.
         */
        HttpParams clientParams = new BasicHttpParams();
        clientParams.setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "UTF-8");
        httpClient = new DefaultHttpClient(connManager, clientParams);

        //We don't need to set timeout for connection manager, since we are doing it below
        // and its enough

        // Get the timeout values set in the runtime
        initializeTimeouts(msgContext, httpClient);

        return httpClient;
    }
}