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

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

Introduction

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

Prototype

public void setContentType(Header header) 

Source Link

Usage

From source file:com.appdynamics.openstack.nova.RestClient.java

static String doPost(String url, String entity, String token) throws Exception {
    HttpClient httpClient = httpClientWithTrustManager();
    StringEntity strEntity = new StringEntity(entity);
    strEntity.setContentType(jsonContentType);

    HttpPost post = new HttpPost(url);
    post.addHeader("accept", xmlContentType);
    post.setEntity(strEntity);// w ww.  j av a  2 s.c  om

    if (token != null) {
        post.addHeader("X-Auth-Token", token);
    }

    HttpResponse response = httpClient.execute(post);

    return getResponseString(httpClient, response);

}

From source file:com.jeecms.common.web.ClientCustomSSL.java

public static String getInSsl(String url, File pkcFile, String storeId, String params, String contentType)
        throws Exception {
    String text = "";
    // ???PKCS12//from  ww w.  ja  va 2  s.  co  m
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    // ?PKCS12?
    FileInputStream instream = new FileInputStream(pkcFile);
    try {
        // PKCS12?(ID)
        keyStore.load(instream, storeId.toCharArray());
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, storeId.toCharArray()).build();
    // Allow TLSv1 protocol only
    // TLS 
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    // httpclientSSLSocketFactory
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {
        HttpPost post = new HttpPost(url);
        StringEntity s = new StringEntity(params, "utf-8");
        if (StringUtils.isBlank(contentType)) {
            s.setContentType("application/xml");
        }
        s.setContentType(contentType);
        post.setEntity(s);
        HttpResponse res = httpclient.execute(post);
        HttpEntity entity = res.getEntity();
        text = EntityUtils.toString(entity, "utf-8");
    } finally {
        httpclient.close();
    }
    return text;
}

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  ww . j  a  v a 2s  .  c  o m*/
    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 . jav  a  2  s . co m
    request.addHeader("accept", "application/json");

    return httpclient.execute(request);

}

From source file:it.sasabz.android.sasabus.classes.hafas.XMLRequest.java

/**
 * Sends a HTTP-Post request to the xml-interface of the hafas travelplanner
 * @param xml is the xml-file containing a xml-request for the hafas travelplanner
 * @return the response of the hafas travelplanner xml interface
 *//*from  www .ja  v  a 2s . c o m*/
private static String execute(String xml) {
    String ret = "";
    if (!haveNetworkConnection()) {
        return ret;
    }
    try {
        HttpClient http = new DefaultHttpClient();
        HttpPost post = new HttpPost(SASAbus.getContext().getString(R.string.xml_server));
        StringEntity se = new StringEntity(xml, HTTP.UTF_8);
        se.setContentType("text/xml");
        post.setEntity(se);

        HttpResponse response = http.execute(post);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            ret = EntityUtils.toString(response.getEntity());
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ret;
}

From source file:com.mber.client.HTTParty.java

private static StringEntity toStringEntity(final JSONObject json) throws UnsupportedEncodingException {
    StringEntity entity = new StringEntity(json.toString(), "UTF-8");
    entity.setContentType("application/json; charset=utf-8");
    return entity;
}

From source file:org.broadinstitute.gatk.utils.help.ForumAPIUtils.java

private static String httpPost(String data, String URL) {
    try {/*from   ww w. j ava 2  s  . co  m*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(URL);

        StringEntity input = new StringEntity(data);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String output = "";
        String line;
        System.out.println("Output from Server .... \n");
        while ((line = br.readLine()) != null) {
            output += (line + '\n');
            System.out.println(line);
        }

        br.close();
        httpClient.getConnectionManager().shutdown();
        return output;

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }
    return null;
}

From source file:com.wbtech.ums.common.NetworkUitlity.java

public static String Post(String url, String data) {

    CommonUtil.printLog("ums", url);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {/* ww  w  .jav  a2s .  c  o m*/
        StringEntity se = new StringEntity("content=" + data, HTTP.UTF_8);
        CommonUtil.printLog("postdata", "content=" + data);
        se.setContentType("application/x-www-form-urlencoded");
        httppost.setEntity(se);
        HttpResponse response = httpclient.execute(httppost);
        int status = response.getStatusLine().getStatusCode();
        CommonUtil.printLog("ums", status + "");
        String returnXML = EntityUtils.toString(response.getEntity());
        Log.d("returnString", URLDecoder.decode(returnXML));
        return URLDecoder.decode(returnXML);

    } catch (Exception e) {
        CommonUtil.printLog("ums", e.toString());
    }
    return null;
}

From source file:org.neo4j.nlp.examples.author.main.java

private static String executePost(String targetURL, String payload) {
    try {//from   w w  w  .j a  v  a  2  s. c  o  m

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(targetURL);

        StringEntity input = new StringEntity(payload);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        StringBuilder output = new StringBuilder();
        while (br.read() != -1) {
            output.append(br.readLine()).append('\n');
        }

        httpClient.getConnectionManager().shutdown();

        return output.toString();

    } catch (IOException e) {

        e.printStackTrace();

    }

    return null;
}

From source file:com.networknt.light.server.handler.loader.Loader.java

public static void login(String host, String userId, String password) throws Exception {
    Map<String, Object> inputMap = new HashMap<String, Object>();
    inputMap.put("category", "user");
    inputMap.put("name", "signInUser");
    inputMap.put("readOnly", false);
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("userIdEmail", userId);
    data.put("password", password);
    data.put("rememberMe", true);
    data.put("clientId", "example@Browser");
    inputMap.put("data", data);

    HttpPost httpPost = new HttpPost(host + "/api/rs");
    StringEntity input = new StringEntity(
            ServiceLocator.getInstance().getMapper().writeValueAsString(inputMap));
    input.setContentType("application/json");
    httpPost.setEntity(input);//from  ww  w .  j a v a2 s.c  o  m
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {

        //System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        //System.out.println("json = " + json);
        Map<String, Object> jsonMap = ServiceLocator.getInstance().getMapper().readValue(json,
                new TypeReference<HashMap<String, Object>>() {
                });
        jwt = (String) jsonMap.get("accessToken");
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        response.close();
    }
}