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:cn.hhh.myandroidserver.response.AndServerPingHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    response.setEntity(new StringEntity("ok", "utf-8"));
}

From source file:org.elasticsearch.upgrades.TokenBackwardsCompatibilityIT.java

public void testGeneratingTokenInOldCluster() throws Exception {
    assumeTrue("this test should only run against the old cluster", clusterType == CLUSTER_TYPE.OLD);
    final StringEntity tokenPostBody = new StringEntity("{\n" + "    \"username\": \"test_user\",\n"
            + "    \"password\": \"x-pack-test-password\",\n" + "    \"grant_type\": \"password\"\n" + "}",
            ContentType.APPLICATION_JSON);
    Response response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(),
            tokenPostBody);/*from  www  .  j  a  v  a 2 s . com*/
    assertOK(response);
    Map<String, Object> responseMap = entityAsMap(response);
    String token = (String) responseMap.get("access_token");
    assertNotNull(token);
    assertTokenWorks(token);

    StringEntity oldClusterToken = new StringEntity("{\n" + "    \"token\": \"" + token + "\"\n" + "}",
            ContentType.APPLICATION_JSON);
    Response indexResponse = client().performRequest("PUT",
            "token_backwards_compatibility_it/doc/old_cluster_token1", Collections.emptyMap(), oldClusterToken);
    assertOK(indexResponse);

    response = client().performRequest("POST", "_xpack/security/oauth2/token", Collections.emptyMap(),
            tokenPostBody);
    assertOK(response);
    responseMap = entityAsMap(response);
    token = (String) responseMap.get("access_token");
    assertNotNull(token);
    assertTokenWorks(token);
    oldClusterToken = new StringEntity("{\n" + "    \"token\": \"" + token + "\"\n" + "}",
            ContentType.APPLICATION_JSON);
    indexResponse = client().performRequest("PUT", "token_backwards_compatibility_it/doc/old_cluster_token2",
            Collections.emptyMap(), oldClusterToken);
    assertOK(indexResponse);
}

From source file:org.apache.kylin.jdbc.TestUtil.java

public static HttpResponse mockHttpResponse(int statusCode, String message, String body) {
    HttpResponse response = Mockito.mock(HttpResponse.class);
    Mockito.when(response.getStatusLine())
            .thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, message));
    Mockito.when(response.getEntity()).thenReturn(new StringEntity(body, StandardCharsets.UTF_8));
    return response;
}

From source file:com.ssy.havefunweb.util.WeixinUtil.java

public static JSONObject doPostStr(String url, String outStr) throws UnsupportedEncodingException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new StringEntity(outStr, "UTF-8"));
    HttpResponse response = httpClient.execute(httpPost);
    String result = EntityUtils.toString(response.getEntity(), "UTF-8");
    JSONObject jsonObject = JSONObject.fromObject(result);
    return jsonObject;
}

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

public static final int put(final HttpClient httpClient, final String url, final String body)
        throws IOException {
    final HttpPut put = new HttpPut(url);
    if (body != null) {
        put.setHeader("Content-Type", Constants.APPLICATION_JSON);
        put.setEntity(new StringEntity(body, "UTF-8"));
    }/*  ww w  . j a va  2 s . co  m*/
    return httpClient.execute(put, new StatusCodeResponseHandler());
}

From source file:HttpConnections.DiffURIBuilder.java

public HttpEntity getEntity() {
    if (this.entity == null) {
        return new StringEntity("", "UTF-8");
    } else {//from  www  .ja v a 2 s  . c  o  m
        return new StringEntity(this.entity, "UTF-8");
    }
}

From source file:text_analytics.OpenCalaisKE.java

private HttpPost postMethod(String content) {
    postMethod = new HttpPost(CALAIS_URL);

    // Set mandatory parameters
    postMethod.addHeader("x-calais-licenseID", licenseID);
    postMethod.addHeader("Content-type", "text/raw; charset=UTF-8");

    // 3. Document content should be passed as the body of the HTTP request.
    StringEntity se = new StringEntity(content, ContentType.create("text/plain", "UTF-8"));
    postMethod.setEntity(se);//from ww  w  .java2 s .  co  m

    // Set response/output format
    //postMethod.addHeader("Accept", "xml/rdf");
    postMethod.addHeader("Accept", "application/json");

    // Enable Social Tags processing
    postMethod.addHeader("enableMetadataType", "SocialTags");

    return postMethod;
}

From source file:com.magnet.plugin.api.requests.PostRequest.java

@Override
protected HttpRequestBase getRequest(RequestModel requestModel) {
    HttpPost httpPost = new HttpPost(requestModel.getTestUrl());
    try {//from   w w w .j a  v a  2  s .  c om
        httpPost.setEntity(new StringEntity(requestModel.getRequest(), "UTF-8"));
    } catch (Exception e) {
    }
    return httpPost;
}

From source file:de.pubflow.server.core.restConnection.WorkflowSender.java

/**
 * Sends a post request to the Workflow engine to use a certain Workflow
 * specified through the given path.//from w  ww. j a  va  2  s . c o m
 * 
 * @param wfCall
 *            The message with all necessary informations to create a new
 *            Workflow
 * @param workflowPath
 *            The path for the specific Workflow to be used.
 * @throws WFRestException
 *             if the connection responses with a HTTP response code other
 *             than 2xx
 */
public static void initWorkflow(WorkflowRestCall wfCall, String workflowPath) throws WFRestException {
    Logger myLogger = LoggerFactory.getLogger(WorkflowSender.class);

    myLogger.info("Trying to use workflow on: " + workflowPath);
    Gson gson = new Gson();
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(workflowPath);
    HttpResponse response = null;

    try {
        StringEntity postingString = new StringEntity(gson.toJson(wfCall), ContentType.APPLICATION_JSON);

        post.setEntity(postingString);
        post.setHeader("Content-type", "application/json;charset=utf-8");
        response = httpClient.execute(post);
        System.out.println(post.getURI());
        myLogger.info("Http response: " + response.toString());

    } catch (Exception e) {
        myLogger.error("Could not deploy new Workflow with ID: " + wfCall.getID());
        myLogger.error(e.toString());
        throw new WFRestException("Workflow could not be started");
    }
    if (response.getStatusLine().getStatusCode() >= 300) {
        throw new WFRestException(
                "The called WorkflowService send status code: " + response.getStatusLine().getStatusCode()
                        + " and error: " + response.getStatusLine().getReasonPhrase());
    }

}

From source file:com.magnet.plugin.api.requests.PutRequest.java

@Override
protected HttpRequestBase getRequest(RequestModel requestModel) {
    HttpPut httpPost = new HttpPut(requestModel.getTestUrl());
    try {/*from   w  w  w . ja  v  a 2 s  . co  m*/
        httpPost.setEntity(new StringEntity(requestModel.getRequest(), "UTF-8"));
    } catch (Exception e) {
    }
    return httpPost;
}