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

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

Introduction

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

Prototype

public StringEntity(String str, Charset charset) 

Source Link

Usage

From source file:xj.property.ums.common.NetworkUitlity.java

public static MyMessage post(String url, String data) {
    // TODO Auto-generated method stub
    CommonUtil.printLog("ums", url);
    String returnContent = "";
    MyMessage message = new MyMessage();
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {// ww  w  .j a  v  a  2 s  .co m
        StringEntity se = new StringEntity(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());
        returnContent = URLDecoder.decode(returnXML);
        switch (status) {
        case 200:
            message.setFlag(true);
            message.setMsg(returnContent);
            break;

        default:
            Log.e("error", status + returnContent);
            message.setFlag(false);
            message.setMsg(returnContent);
            break;
        }
    } catch (Exception e) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("err", e.toString());
            returnContent = jsonObject.toString();
            message.setFlag(false);
            message.setMsg(returnContent);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }

    }
    CommonUtil.printLog("UMSAGENT", message.getMsg());
    return message;
}

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

public static MyMessage post(String url, String data) {
    // TODO Auto-generated method stub
    CommonUtil.printLog("ums", url);
    String returnContent = "";
    MyMessage message = new MyMessage();
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {//from  w  w  w.  j a v a  2 s. com
        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());
        returnContent = URLDecoder.decode(returnXML);
        switch (status) {
        case 200:
            message.setFlag(true);
            message.setMsg(returnContent);
            break;

        default:
            Log.e("error", status + returnContent);
            message.setFlag(false);
            message.setMsg(returnContent);
            break;
        }
    } catch (Exception e) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("err", e.toString());
            returnContent = jsonObject.toString();
            message.setFlag(false);
            message.setMsg(returnContent);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }

    }
    CommonUtil.printLog("UMSAGENT", message.getMsg());
    return message;
}

From source file:org.apache.heron.integration_topology_test.core.HttpUtils.java

public static int httpJsonPost(String newHttpPostUrl, String jsonData) throws IOException, ParseException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(newHttpPostUrl);

    StringEntity requestEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON);

    post.setEntity(requestEntity);/*from  w  w  w . j a  v a2  s.  c o m*/
    HttpResponse response = client.execute(post);

    return response.getStatusLine().getStatusCode();
}

From source file:com.xl.service.OperationService.java

public static void addSampleSink(RestClient client, String fileName) throws IOException {

    HttpEntity entity = new StringEntity(ReadExcelSampleSilk.read(fileName), ContentType.APPLICATION_JSON);
    Response indexResponse = getRestClient().performRequest("PUT", "/test/sample/1",
            Collections.<String, String>emptyMap(), entity);

    System.out//from  ww w . j a va  2s .c  o m
            .println("respone :::: from server ::::::::::::::" + indexResponse.getStatusLine().getStatusCode());

}

From source file:com.ysheng.auth.common.restful.util.JsonSerializer.java

/**
 * Serializes an object to JSON string./*from w ww. j  av  a 2s .c  o m*/
 *
 * @param object The object to be serialized.
 * @return A JSON string that contains the serialized object.
 * @throws JsonProcessingException The error that contains detail information.
 */
public static HttpEntity serialize(Object object) throws JsonProcessingException {
    String serialized = objectMapper.writeValueAsString(object);

    return new StringEntity(serialized, ContentType.APPLICATION_JSON);
}

From source file:com.spokenpic.net.RestClientPut.java

@Override
protected RestResult doPost() {
    HttpPut httpPut = new HttpPut(getSchemeServer() + this.uri);
    setupHttp(httpPut);//from   w  w  w  .j  a v  a  2 s. c  om
    try {
        StringEntity data = new StringEntity(this.data, "UTF-8");
        data.setChunked(false);
        httpPut.setEntity(data);
        httpPut.setHeader("Content-type", "application/json");
        HttpResponse httpResponse = HttpManager.execute(httpPut);
        return returnResponse(httpResponse);
    } catch (Exception e) {
        Log.d("RestClientFilePUT", "Error doPost: " + e.toString());
        return errorResponse("Fatal error during PUT");
    }
}

From source file:com.kingmed.dp.aperio.DsClient.java

public static String logon() throws Exception {
    String token = null;// w ww . j  a va  2 s.  co  m
    String url = "http://192.168.180.132:86/Aperio.Security/Security2.asmx";
    String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://www.aperio.com/webservices/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> <SOAP-ENV:Body> <Logon><Token>leQJYfWQ6wv_tJa6hhBZlWwgrRZ-mDywnfb9F4EfC1752Pt07NZDEGvFNYYPvpxkN0IvPTrPi0M=</Token><LoginName>gzuser</LoginName><Password>gzking</Password></Logon></SOAP-ENV:Body> </SOAP-ENV:Envelope> ";

    HttpClient hc = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("Content-Type", "text/xml;charset=utf-8");
    httpPost.addHeader("SOAPAction", "http://www.aperio.com/webservices/#Logon");
    StringEntity myEntity = new StringEntity(body, ContentType.create("text/xml", "UTF-8"));
    httpPost.setEntity(myEntity);
    System.out.println(myEntity.getContentType());
    System.out.println("Content-Length" + myEntity.getContentLength());
    HttpResponse res = null;
    res = (CloseableHttpResponse) hc.execute(httpPost);
    HttpEntity entity = res.getEntity();
    System.out.println(EntityUtils.toString(entity));

    return token;
}

From source file:com.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java

public static String sendPost(String serverURL, String postBody) throws Exception {

    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost post = new HttpPost(serverURL);

    StringEntity entity = new StringEntity(postBody, "UTF-8");

    post.setEntity(entity);//from   w  w  w  .  j av  a2 s.  co  m

    HttpResponse response = client.execute(post);
    // System.out.println("\nSending 'POST' request to URL : " + serverURL);

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

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    client.close();
    // System.out.println(result.toString());
    return result.toString();
}

From source file:org.ojbc.util.helper.HttpUtils.java

/**
 * Send the specified payload to the specified http endpoint via POST.
 * @param payload/* w w  w  .  j  a v  a  2  s.  com*/
 * @param url
 * @return the http response
 * @throws Exception
 */
public static String post(String payload, String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setEntity(new StringEntity(payload, Consts.UTF_8));
    HttpResponse response = client.execute(post);
    HttpEntity reply = response.getEntity();
    StringWriter sw = new StringWriter();
    BufferedReader isr = new BufferedReader(new InputStreamReader(reply.getContent()));
    String line;
    while ((line = isr.readLine()) != null) {
        sw.append(line);
    }
    sw.close();
    return sw.toString();
}

From source file:com.calclab.emite.core.client.services.android.AndroidConnector.java

public static void send(final String httpBase, final String request, final ConnectorCallback listener) {
    Runnable r = new Runnable() {
        public void run() {
            try {
                HttpPost method = new HttpPost(httpBase);
                method.addHeader("Content-Type", "text/xml; charset=\"utf-8\"");
                method.setEntity(new StringEntity(request, "utf-8"));

                final HttpResponse response = new DefaultHttpClient().execute(method);
                handler.post(new Runnable() {
                    public void run() {
                        try {
                            HttpEntity entity = response.getEntity();
                            String content = readInputStream(entity.getContent());
                            listener.onResponseReceived(response.getStatusLine().getStatusCode(), content);
                        } catch (Throwable e) {
                            listener.onError(request, e);
                        }/*ww w .  j  a v  a  2  s  .com*/
                    }
                });
            } catch (final Throwable e) {
                handler.post(new Runnable() {
                    public void run() {
                        listener.onError(request, e);
                    }
                });
            }
        }
    };
    new Thread(r).start();
}