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

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

Introduction

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

Prototype

String HTTP_CONTENT_CHARSET

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

Click Source Link

Usage

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

public static String lookUpUpcDatabase(String ean) {

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

        HttpClient httpclient = new DefaultHttpClient();

        try {//from   w w  w  .jav a2  s  .c  o  m

            HttpGet httpget = new HttpGet("http://www.upcdatabase.com/item/" + ean);
            httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

            // 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;
                    }
                }
            };

            String responseBody = new String(httpclient.execute(httpget, handler), "UTF-8");

            // return null if not found
            if (responseBody.contains("Item Not Found"))
                return null;
            else if (responseBody.contains("Item Record")) {

                // Parse response document
                Matcher matcher = Pattern.compile("<tr><td>Description<\\/td><td><\\/td><td>(.*)<\\/td><\\/tr>")
                        .matcher(responseBody);
                if (matcher.find()) {
                    res = matcher.group(1);
                }

            }

        } 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:ch.mbae.pusher.transport.HttpClientPusherTransport.java

/**
 * create a  http transportn using a single http client
 * for all requests/*  w  w  w  .j ava 2  s. c  o m*/
 */
public HttpClientPusherTransport() {
    ClientConnectionManager cm = new ThreadSafeClientConnManager();

    this.httpClient = new DefaultHttpClient(cm);
    this.httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); // set default to HTTP 1.1
    this.httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
}

From source file:com.vintank.slack4j.webhook.SlackWebHook.java

public void send(Payload payload) throws IOException {
    HttpPost post = new HttpPost(url);

    try {// www  .  j a  v  a2s.c o m
        post.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Slack4j/1.0");
        post.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 5000);
        client.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 5000);

        List<NameValuePair> nvps = new ArrayList<NameValuePair>(1);
        nvps.add(new BasicNameValuePair("payload", MAPPER.writeValueAsString(payload)));

        post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

        client.execute(post);
    } finally {
        post.releaseConnection();
    }
}

From source file:org.apache.olingo.samples.client.core.http.ParametersHttpUriRequestFactory.java

@Override
public HttpUriRequest create(final HttpMethod method, final URI uri) {
    final HttpUriRequest request = super.create(method, uri);

    request.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
    request.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

    final int timeout = 1000;
    HttpConnectionParams.setConnectionTimeout(request.getParams(), timeout);
    HttpConnectionParams.setSoTimeout(request.getParams(), timeout);

    return request;
}

From source file:org.apache.olingo.samples.client.core.http.ParametersHttpClientFactory.java

@Override
public DefaultHttpClient create(final HttpMethod method, final URI uri) {
    final DefaultHttpClient httpClient = super.create(method, uri);

    httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
    httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

    final int timeout = 1000;
    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), timeout);
    HttpConnectionParams.setSoTimeout(httpClient.getParams(), timeout);

    return httpClient;
}

From source file:org.bimserver.client.json.JsonSocketReflectorFactory.java

public JsonSocketReflectorFactory(SServicesMap servicesMap) {
    if (servicesMap == null) {
        throw new IllegalArgumentException("servicesMap cannot be null");
    }//www .j ava  2  s  .c  o m
    this.servicesMap = servicesMap;

    connectionManager = new PoolingClientConnectionManager();
    connectionManager.setDefaultMaxPerRoute(5);
    connectionManager.setMaxTotal(20);

    httpclient = new DefaultHttpClient(connectionManager);
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
}

From source file:com.traffic.common.utils.http.HttpClientUtils.java

/**
 * ,result""/*w w  w .  j a  va 2s.c  om*/
 * 
 * @param url
 * @param param
 * @return
 */
public static String httpPost(String url, String paramvalue) {
    logger.info("httpPost URL [" + url + "] start ");
    if (logger.isDebugEnabled()) {
        logger.debug("httpPost body :" + paramvalue);
    }
    logger.info("httpPost body :" + paramvalue);
    HttpClient httpclient = null;
    HttpPost httpPost = null;
    HttpResponse response = null;
    HttpEntity entity = null;
    String result = "";

    try {
        httpclient = HttpConnectionManager.getHttpClient();
        // cookie---??
        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
        httpPost = new HttpPost(url);
        // ???
        for (Entry<String, String> entry : headers.entrySet()) {
            httpPost.setHeader(entry.getKey(), entry.getValue());
        }

        StringEntity stringEntity = new StringEntity(paramvalue, "UTF-8");
        httpPost.setEntity(stringEntity);

        // 
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 40000);
        // ?
        HttpConnectionParams.setSoTimeout(httpPost.getParams(), 200000);
        response = httpclient.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            logger.error("HttpStatus ERROR" + "Method failed: " + response.getStatusLine());
            return "";
        } else {
            entity = response.getEntity();
            if (null != entity) {
                byte[] bytes = EntityUtils.toByteArray(entity);
                result = new String(bytes, "UTF-8");
            } else {
                logger.error("httpPost URL [" + url + "],httpEntity is null.");
            }
            return result;
        }
    } catch (Exception e) {
        logger.error("httpPost URL [" + url + "] error, ", e);
        return "";
    }
}

From source file:me.chunsheng.ebooks.hackerearth.api.requests.BaseRequest.java

protected String sendRequest(final String endpoint, final List<NameValuePair> options) {
    try {//from   ww  w  .  j  a va2  s  . co  m
        HttpPost httpPost = new HttpPost(endpoint);
        httpPost.setEntity(new UrlEncodedFormEntity(options));
        httpPost.setHeader(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF_8");
        //httpPost.setHeader("Content-type", "application/json");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer resultBuffer = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            resultBuffer.append(line);
        }
        return resultBuffer.toString();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.impetus.client.couchdb.utils.CouchDBTestUtils.java

/**
 * Initiate http client./*from   w w w . j a  v a  2 s .co  m*/
 * 
 * @param kunderaMetadata
 *            the kundera metadata
 * @param persistenceUnit
 *            the persistence unit
 * @return the http client
 */
public static HttpClient initiateHttpClient(final KunderaMetadata kunderaMetadata, String persistenceUnit) {
    PersistenceUnitMetadata pumMetadata = KunderaMetadataManager.getPersistenceUnitMetadata(kunderaMetadata,
            persistenceUnit);

    SchemeSocketFactory ssf = null;
    ssf = PlainSocketFactory.getSocketFactory();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    int port = Integer.parseInt(pumMetadata.getProperty(PersistenceProperties.KUNDERA_PORT));
    String host = pumMetadata.getProperty(PersistenceProperties.KUNDERA_NODES);
    String userName = pumMetadata.getProperty(PersistenceProperties.KUNDERA_USERNAME);
    String password = pumMetadata.getProperty(PersistenceProperties.KUNDERA_PASSWORD);

    schemeRegistry.register(new Scheme("http", port, ssf));
    PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry);
    HttpClient httpClient = new DefaultHttpClient(ccm);

    try {
        // Http params
        httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

        // basic authentication
        if (userName != null && password != null) {
            ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(new AuthScope(host, port),
                    new UsernamePasswordCredentials(userName, password));
        }
        // request interceptor
        ((DefaultHttpClient) httpClient).addRequestInterceptor(new HttpRequestInterceptor() {
            public void process(final HttpRequest request, final HttpContext context) throws IOException {

            }
        });
        // response interceptor
        ((DefaultHttpClient) httpClient).addResponseInterceptor(new HttpResponseInterceptor() {
            public void process(final HttpResponse response, final HttpContext context) throws IOException {

            }
        });
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    return httpClient;
}

From source file:gov.in.bloomington.georeporter.models.Open311.java

/**
 * Lazy load an Http client//  ww  w  .j  a  va 2s . co  m
 * 
 * @return
 * DefaultHttpClient
 */
private static DefaultHttpClient getClient() {
    if (mClient == null) {
        mClient = new DefaultHttpClient();
        mClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
        mClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        mClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT);
        mClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT);
    }
    return mClient;
}