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:com.ilearnrw.reader.utils.HttpHelper.java

public static HttpResponse post(String url, String data) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = null;//  www . j  a v  a 2 s.co m

    //HttpConnectionParams.setSoTimeout(client.getParams(), 25000);

    HttpPost post = new HttpPost(url);
    post.setHeader("Accept", "application/json");
    post.setHeader("Authorization", "Basic " + authString);
    post.setHeader("Content-Type", "application/json;charset=utf-8");

    try {
        post.setEntity(new StringEntity(data, HTTP.UTF_8));
        response = client.execute(post);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return response;
}

From source file:io.selendroid.server.util.HttpClientUtil.java

public static HttpResponse executeRequestWithPayload(String uri, int port, HttpMethod method, String payload)
        throws Exception {
    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(method.getName(), uri);
    request.setEntity(new StringEntity(payload, "UTF-8"));

    return getHttpClient().execute(new HttpHost("localhost", port), request);
}

From source file:com.huawei.ais.demo.ocr.DriverLicenseDemo.java

private static void driverLicenseDemo() throws IOException {
    ////from   ww  w.ja  va2s.c  o  m
    // 1. ClientContextUtils, ?AIS??, 
    // ??
    // 
    AisAccess service = new AisAccess(ClientContextUtils.getAuthInfo());

    //
    // 1.a ???AIS????, 
    // ???ClientContextUtils????(ProxyHostInfo)
    //
    //AisAccess service = new AisAccessWithProxy(ClientContextUtils.getAuthInfo(), ClientContextUtils.getProxyHost());

    try {
        //
        // 2.????
        //
        String uri = "/v1.0/ocr/driver-license";
        byte[] fileData = FileUtils.readFileToByteArray(new File("data/driver-license-demo.png"));
        String fileBase64Str = Base64.encodeBase64String(fileData);

        JSONObject json = new JSONObject();
        json.put("image", fileBase64Str);
        StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");

        // 3.??uri?, ????
        // ??JSON?, POST?
        HttpResponse response = service.post(uri, stringEntity);

        // 4.?????200, ?, ?
        ResponseProcessUtils.processResponseStatus(response);

        // 5.???
        ResponseProcessUtils.processResponse(response);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        // 6.?
        service.close();
    }
}

From source file:com.huawei.ais.demo.ocr.VehicleLicenseDemo.java

private static void vehicleLicenseDemo() throws IOException {
    ///*from  w  w  w. j a va 2 s .c o  m*/
    // 1. ClientContextUtils, ?AIS??, 
    // ??
    //       
    AisAccess service = new AisAccess(ClientContextUtils.getAuthInfo());

    //
    // 1.a ???AIS????, 
    // ???ClientContextUtils????(ProxyHostInfo)
    //
    //AisAccess service = new AisAccessWithProxy(ClientContextUtils.getAuthInfo(), ClientContextUtils.getProxyHost());

    try {
        //
        // 2.????
        //
        String uri = "/v1.0/ocr/vehicle-license";
        byte[] fileData = FileUtils.readFileToByteArray(new File("data/vehicle-license-demo.png"));
        String fileBase64Str = Base64.encodeBase64String(fileData);

        JSONObject json = new JSONObject();
        json.put("image", fileBase64Str);
        StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");

        // 3.??uri?, ????
        // ??JSON?, POST?
        HttpResponse response = service.post(uri, stringEntity);

        // 4.?????200, ?, ?
        ResponseProcessUtils.processResponseStatus(response);

        // 5.???
        ResponseProcessUtils.processResponse(response);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        // 6.?
        service.close();
    }
}

From source file:com.srotya.tau.ui.BapiLoginDAO.java

public static Entry<String, String> authenticate(String authURL, String username, String password)
        throws Exception {
    CloseableHttpClient client = Utils.buildClient(authURL, 3000, 5000);
    HttpPost authRequest = new HttpPost(authURL);
    Gson gson = new Gson();
    JsonObject obj = new JsonObject();
    obj.addProperty(USERNAME, username);
    obj.addProperty(PASSWORD, password);
    StringEntity entity = new StringEntity(gson.toJson(obj), ContentType.APPLICATION_JSON);
    authRequest.setEntity(entity);/*from  w  w w. j  av a2 s  .co  m*/
    CloseableHttpResponse response = client.execute(authRequest);
    if (response.getStatusLine().getStatusCode() == 200) {
        String tokenPair = EntityUtils.toString(response.getEntity());
        JsonArray ary = gson.fromJson(tokenPair, JsonArray.class);
        obj = ary.get(0).getAsJsonObject();
        String token = obj.get(X_SUBJECT_TOKEN).getAsString();
        String hmac = obj.get(HMAC).getAsString();
        return new AbstractMap.SimpleEntry<String, String>(token, hmac);
    } else {
        System.err.println("Login failed:" + response.getStatusLine().getStatusCode() + "\t"
                + response.getStatusLine().getReasonPhrase());
        return null;
    }
}

From source file:com.roncoo.pay.permission.utils.RoncooHttpClientUtils.java

/**
 *  API//from w w  w  . ja  v a2s .  c o m
 * 
 * @param parameters
 * @return
 */
@SuppressWarnings({ "resource", "deprecation" })
public static String post(String url, String parameters) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost method = new HttpPost(url);
    String body = null;

    if (method != null & parameters != null && !"".equals(parameters.trim())) {
        try {

            // NameValuePair??
            method.addHeader("Content-type", "application/json; charset=utf-8");
            method.setHeader("Accept", "application/json");
            method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8")));

            HttpResponse response = httpClient.execute(method);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                return "1";// 1
            }

            // Read the response body
            body = EntityUtils.toString(response.getEntity());

        } catch (IOException e) {
            // 
            return "2";
        } finally {
        }

    }
    return body;
}

From source file:com.github.rnewson.couchdb.lucene.couchdb.HttpUtils.java

public static final String post(final HttpClient httpClient, final String url, final JSONObject body)
        throws IOException {
    final HttpPost post = new HttpPost(url);
    post.setHeader("Content-Type", Constants.APPLICATION_JSON);
    post.setEntity(new StringEntity(body.toString(), "UTF-8"));
    return execute(httpClient, post);
}

From source file:io.selendroid.standalone.server.util.HttpClientUtil.java

public static HttpResponse executeRequestWithPayload(String uri, int port, HttpMethod method, String payload)
        throws Exception {
    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(method.name(), uri);
    request.setEntity(new StringEntity(payload, "UTF-8"));

    return getHttpClient().execute(new HttpHost("localhost", port), request);
}

From source file:net.locosoft.fold.neo4j.internal.Neo4jRestUtil.java

public static JsonObject doPostJson(String uri, JsonObject content) {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        HttpPost httpPost = new HttpPost(uri);
        httpPost.addHeader("Content-Type", "application/json");
        StringEntity stringEntity = new StringEntity(content.toString(), "UTF-8");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = httpClient.execute(httpPost);

        String bodyText = EntityUtils.toString(response.getEntity());
        JsonObject jsonObject = JsonObject.readFrom(bodyText);
        return jsonObject;
    } catch (Exception ex) {
        ex.printStackTrace();//from  w  ww .ja v a2 s.c o m
    }
    return null;
}

From source file:io.fabric8.maven.docker.access.util.RequestUtil.java

public static HttpUriRequest newPost(String url, String body) {
    HttpPost post = new HttpPost(url);
    if (body != null) {
        post.setEntity(new StringEntity(body, Charset.defaultCharset()));
    }//w ww. j a  v a2s .c om
    return addDefaultHeaders(post);
}