Example usage for org.apache.http.entity StringEntity setContentEncoding

List of usage examples for org.apache.http.entity StringEntity setContentEncoding

Introduction

In this page you can find the example usage for org.apache.http.entity StringEntity setContentEncoding.

Prototype

public void setContentEncoding(Header header) 

Source Link

Usage

From source file:com.google.developers.gdgfirenze.mockep.AndroidSimulator.java

private static void postData(String url, JSONObject jsonSamplePacket)
        throws ClientProtocolException, IOException {

    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);
    HttpClient httpclient = new DefaultHttpClient(myParams);

    HttpPost httppost = new HttpPost(url.toString());
    httppost.setHeader("Content-type", "application/json");

    StringEntity se = new StringEntity(jsonSamplePacket.toString());
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    httppost.setEntity(se);/*from  w  w w  .ja v a 2  s  . c  om*/

    HttpResponse response = httpclient.execute(httppost);

    String temp = EntityUtils.toString(response.getEntity());
    System.out.println("JSON post response: " + temp);
}

From source file:es.ugr.swad.swadroid.webservices.RestEasy.java

public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding("UTF-8");
    s.setContentType("application/json");

    request.setEntity(s);//from w w  w  .j  a v  a  2s  . c  om
    request.addHeader("accept", "application/json");

    return httpclient.execute(request);
}

From source file:es.ugr.swad.swadroid.webservices.RestEasy.java

public static HttpResponse doPut(String url, JSONObject c) throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPut request = new HttpPut(url);
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding("UTF-8");
    s.setContentType("application/json");

    request.setEntity(s);/*from w  w  w  .  j a  va2 s . c  o  m*/
    request.addHeader("accept", "application/json");

    return httpclient.execute(request);

}

From source file:com.portfolio.data.utils.PostForm.java

public static boolean updateResource(String sessionid, String backend, String uuid, String lang, String json)
        throws Exception {
    /// Parse and create xml from JSON
    JSONObject files = (JSONObject) JSONValue.parse(json);
    JSONArray array = (JSONArray) files.get("files");

    if ("".equals(lang) || lang == null)
        lang = "fr";

    JSONObject obj = (JSONObject) array.get(0);
    String ressource = "";
    String attLang = " lang=\"" + lang + "\"";
    ressource += "<asmResource>" + "<filename" + attLang + ">" + obj.get("name") + "</filename>" + // filename
            "<size" + attLang + ">" + obj.get("size") + "</size>" + "<type" + attLang + ">" + obj.get("type")
            + "</type>" +
            //      obj.get("url");   // Backend source, when there is multiple backend
            "<fileid" + attLang + ">" + obj.get("fileid") + "</fileid>" + "</asmResource>";

    /// Send data to resource
    /// Server + "/resources/resource/file/" + uuid +"?lang="+ lang
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*w ww .  j  av  a  2  s .co m*/
        HttpPut put = new HttpPut("http://" + backend + "/rest/api/resources/resource/" + uuid);
        put.setHeader("Cookie", "JSESSIONID=" + sessionid); // So that the receiving servlet allow us

        StringEntity se = new StringEntity(ressource);
        se.setContentEncoding("application/xml");
        put.setEntity(se);

        CloseableHttpResponse response = httpclient.execute(put);

        try {
            HttpEntity resEntity = response.getEntity();
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }

    return false;
}

From source file:com.pyz.tool.weixintool.util.HttpTool.java

public static String postRequest(String url, String content) throws Exception {
    String result = "";
    HttpClient httpClient = new DefaultHttpClient();
    try {/*from   ww w  .j  ava 2s. c  om*/
        HttpPost httppost = new HttpPost(url);
        StringEntity myEntity = new StringEntity(content, "utf-8");
        myEntity.setContentEncoding("utf-8");
        myEntity.setContentType("application/json");
        httppost.setEntity(myEntity);
        HttpResponse httpresponse = httpClient.execute(httppost);
        HttpEntity entity = httpresponse.getEntity();
        result = EntityUtils.toString(entity);
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;

}

From source file:org.openo.nfvo.emsdriver.northbound.client.HttpClientUtil.java

public static String doPost(String url, String json, String charset) {
    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;/*  w ww  . j a v a2s .c  o m*/
    String result = null;
    try {
        httpClient = HttpClientFactory.getSSLClientFactory();
        httpPost = new HttpPost(url);
        if (null != json) {
            StringEntity s = new StringEntity(json);
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json"); // set contentType
            httpPost.setEntity(s);
        }
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception e) {
            log.error("httpClient.execute(httpPost) is fail", e);
        } finally {
            if (response != null) {
                response.close();
            }
        }
    } catch (Exception e) {
        log.error("doPost is fail ", e);
    } finally {
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (IOException e) {
            }
        }

    }
    return result;
}

From source file:org.openo.nfvo.monitor.dac.util.APIHttpClient.java

@SuppressWarnings({ "resource" })
public static String doPost2Str(String url, JSONObject json, String token) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    String response = null;//w w  w . j a  v  a 2s.co  m
    try {
        if (null != json) {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json"); // set contentType
            post.setEntity(s);
        }
        if (!Global.isEmpty(token)) {
            post.addHeader("X-Auth-Token", token);
        }
        HttpResponse res = client.execute(post);
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK
                || res.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
            String result = EntityUtils.toString(res.getEntity());
            logger.info("post result is :{}", result);
            if (!Global.isEmpty(result)) {
                response = result;
            } else {
                response = null;
            }
        }
    } catch (Exception e) {
        logger.error("Exception", e);
    }
    return response;
}

From source file:org.openo.nfvo.monitor.dac.util.APIHttpClient.java

@SuppressWarnings({ "resource" })
public static JSONObject doPost(String url, JSONObject json, String token) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    JSONObject response = null;/* w ww  . j  a v a2 s .c  o m*/
    try {
        if (null != json) {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
        }
        if (!Global.isEmpty(token)) {
            post.addHeader("X-Auth-Token", token);
        }
        HttpResponse res = client.execute(post);
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK
                || res.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
            String result = EntityUtils.toString(res.getEntity());
            logger.info("post result is :{}", result);
            if (!Global.isEmpty(result)) {
                response = JSONObject.fromObject(result);
            } else {
                response = null;
            }
        }
    } catch (Exception e) {
        logger.error("Exception", e);
    }
    return response;
}

From source file:org.surveydroid.android.coms.WebClient.java

private static synchronized boolean postJsonToUrl(Context ctxt, String url, String value, boolean firstCall) {
    HttpClient httpclient = getClient(ctxt);
    HttpPost httpPost = new HttpPost(url);

    try {//from  w ww  .  j a  va2s. c o  m
        StringEntity se = new StringEntity(value);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpPost.setEntity(se);

        // Execute HTTP Post Request
        HttpResponse response;
        try {
            response = httpclient.execute(httpPost);
        } catch (SSLException e) {
            //make sure this isn't the recursive call
            if (!firstCall)
                throw new ApiException("Untrusted certificate");
            //switch to using the other client
            swapKeyStore(ctxt);
            //the just do a recursive call
            return postJsonToUrl(ctxt, url, value, false);
        }

        Util.d(null, TAG, "Content: " + getInputStreamAsString(ctxt, response.getEntity().getContent()));

        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() == HTTP_STATUS_OK)
            return true;
    } catch (Exception e) {
        Util.e(null, TAG, Util.fmt(e));
    }
    return false;
}

From source file:org.openo.nfvo.monitor.umc.util.APIHttpClient.java

@SuppressWarnings({ "resource" })
public static String doPost2Str(String url, JSONObject json, String token) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    String response = null;/*from ww  w  .j av a 2 s.  c  om*/
    try {
        if (null != json) {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json"); // set contentType
            post.setEntity(s);
        }
        if (!Global.isEmpty(token)) {
            post.addHeader("X-Auth-Token", token);
        }
        HttpResponse res = client.execute(post);
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK
                || res.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
            String result = EntityUtils.toString(res.getEntity());
            if (!Global.isEmpty(result)) {
                response = result;
            } else {
                response = null;
            }
        }
    } catch (Exception e) {
        logger.error("Exception", e);
    }
    return response;
}