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:net.nordist.lloydproof.HttpJSONClient.java

public void sendRequest(JSONObject json) throws IOException, JSONException {
    StringEntity entity = new StringEntity(json.toString(), HTTP.UTF_8);
    entity.setContentType("application/json");
    httpRequest.setEntity(entity);/*from w  ww  .  ja  va  2  s  . c o m*/
    httpResponse = httpClient.execute(httpRequest);
}

From source file:com.strato.hidrive.api.connection.httpgateway.request.JSONPutRequest.java

protected HttpRequestBase createHttpRequest(String requestUri, HttpRequestParamsVisitor<?> visitor)
        throws UnsupportedEncodingException {
    HttpPut httpPost = new HttpPut(requestUri);
    String jsonString = "";
    if (jsonObject != null) {
        jsonString = jsonObject.toString();
    } else if (jsonArray != null) {
        jsonString = jsonArray.toString();
    }// w w w  . j a v  a  2  s .c  om
    StringEntity stringEntity = new StringEntity(jsonString);
    stringEntity.setContentType(CONTENT_TYPE_APPLICATION_JSON);
    httpPost.setEntity(stringEntity);
    return httpPost;
}

From source file:com.dv.sharer.mobile.rest.impl.ImageAndroidRestClient.java

protected HttpPost getUploadPOST(String urlStr, String data) throws IOException {
    HttpPost httppost = new HttpPost(urlStr);
    httppost.setHeader("Content-Type", "text/plain");
    StringEntity entity = new StringEntity(data);
    entity.setContentType("application/json");
    httppost.addHeader("Accept", "application/json");
    httppost.setEntity(entity);/*from   w  ww.  j  a v a  2  s .com*/
    return httppost;
}

From source file:com.automatski.autosim.rest.SampleRestConnection.java

@Override
public Object send(Object arg0) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(config.url);

    StringEntity input = new StringEntity((String) arg0);
    input.setContentType("application/json");
    postRequest.setEntity(input);//from w  ww.  j av  a  2s.  c  om

    HttpResponse response = httpClient.execute(postRequest);
    response.getEntity().consumeContent();
    // return nothing. we are as of now not interested in the response
    return null;
}

From source file:org.apache.camel.component.cxf.jaxrs.CxfRsConsumerWithBeanTest.java

private void sendPutRequest(String uri) throws Exception {
    HttpPut put = new HttpPut(uri);
    StringEntity entity = new StringEntity("string");
    entity.setContentType("text/plain");
    put.setEntity(entity);/*w  w w .j a va 2s .  com*/
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();

    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("c20string", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}

From source file:com.strato.hidrive.api.connection.httpgateway.request.PostRequest.java

@SuppressWarnings("unchecked")
protected HttpRequestBase createHttpRequest(String requestUri, HttpRequestParamsVisitor<?> visitor)
        throws UnsupportedEncodingException {
    HttpPost httpPost = new HttpPost(requestUri);
    //There used StringEntity and some string manipulations instead of UrlEncodedFormEntity due to problem in url encoding ("+" instead "%20")
    //details: http://stackoverflow.com/questions/7915029/how-to-encode-space-as-20-in-urlencodedformentity-while-executing-apache-httppo
    String entityValue = URLEncodedUtils.format((List<NameValuePair>) visitor.getHttpRequestParams(),
            HTTP.UTF_8);//from   w w  w. j  a  va2s  .co m
    entityValue = entityValue.replaceAll("\\+", "%20");
    StringEntity entity = new StringEntity(entityValue, HTTP.UTF_8);
    entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
    httpPost.setEntity(entity);
    //original code:
    //httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>) visitor.getHttpRequestParams(), HTTP.UTF_8));
    return httpPost;
}

From source file:org.hydracache.server.httpd.handler.BaseJsonServiceAction.java

private void generateResponse(HttpResponse response, String jsonString) throws UnsupportedEncodingException {
    StringEntity body = new StringEntity(jsonString);

    body.setContentType(PLAIN_TEXT_RESPONSE_CONTENT_TYPE);

    response.setEntity(body);/* w  w  w  .  jav  a2 s  .c  o m*/
}

From source file:com.nominanuda.web.http.FormEncodingUtf8Test.java

private String g(String v, String ct) throws IOException {
    StringEntity se = new StringEntity("f=" + v);
    if (ct != null) {
        se.setContentType(ct);
    }/*w w w.  j a  va2 s  . co m*/
    List<NameValuePair> pairs = h.parseEntityWithDefaultUtf8(se);
    String vv = pairs.get(0).getValue();
    return vv;
}

From source file:org.elasticsearch.querydoge.helper.HttpHelper.java

public String postAndReturnBody(String path, String message) throws ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(baseUrl + path);
    StringEntity entity = new StringEntity(message, HTTP.UTF_8);
    entity.setContentType("application/x-www-form-urlencoded");
    httpPost.setEntity(entity);/*from  ww  w.ja v a  2 s. co m*/

    System.out.println(httpPost);
    System.out.println(message);

    return httpclient.execute(httpPost, responseHandler);
}

From source file:org.elasticsearch.querydoge.helper.HttpHelper.java

public String putAndReturnBody(String path, String message) throws ClientProtocolException, IOException {
    HttpPut httpPut = new HttpPut(baseUrl + path);
    StringEntity entity = new StringEntity(message, HTTP.UTF_8);
    entity.setContentType("application/x-www-form-urlencoded");
    httpPut.setEntity(entity);/*from   w  w  w.  j  a va 2 s . c om*/

    System.out.println(httpPut);
    System.out.println(message);

    return httpclient.execute(httpPut, responseHandler);
}