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

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

Introduction

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

Prototype

public Header getContentType() 

Source Link

Usage

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

public static String logon() throws Exception {
    String token = null;// w  w  w . ja v a  2s  .  c  o 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.rukman.emde.smsgroups.client.NetworkUtilities.java

private static String doPostRequest(String url, String authToken, JSONObject parameters)
        throws IOException, JSONException {
    final HttpPost post = new HttpPost(url);
    if (!TextUtils.isEmpty(authToken)) {
        post.addHeader(PARAM_AUTHORIZATION, authToken);
    }/*from  w  w  w  . j  a va2  s.  c om*/
    final StringEntity entity = new StringEntity(parameters.toString());
    entity.setContentType("application/json");
    post.addHeader(entity.getContentType());
    post.addHeader(new BasicHeader(GMS_HEADER_API_KEY, GMS_ANDROID_API_KEY));
    post.setEntity(entity);
    return executeHttpRequest(post);
}

From source file:io.cloudslang.content.httpclient.build.EntityBuilderTest.java

@Test
public void buildEntity() {
    HttpEntity httpEntity = entityBuilder.setBody("testBody").buildEntity();
    assertThat(httpEntity, instanceOf(StringEntity.class));
    StringEntity stringEntity = (StringEntity) httpEntity;
    assertNull(stringEntity.getContentType());
}

From source file:de.devbliss.apitester.factory.impl.EntityBuilderTest.java

@Test
public void buildEntityFromStringPayload() throws Exception {
    final String payload = "spitting cobra";
    StringEntity result = entityBuilder.buildEntity(payload);
    assertEquals("text/plain", result.getContentType().getValue());
    assertEquals(payload, IOUtils.toString(result.getContent()));
}

From source file:io.cloudslang.content.httpclient.build.EntityBuilderTest.java

@Test
public void buildEntityWithContentType() {
    ContentType parsedContentType = ContentType.parse(CONTENT_TYPE);
    HttpEntity httpEntity = entityBuilder.setBody("testBody").setContentType(parsedContentType).buildEntity();
    assertThat(httpEntity, instanceOf(StringEntity.class));
    StringEntity stringEntity = (StringEntity) httpEntity;
    assertEquals(CONTENT_TYPE, stringEntity.getContentType().getValue());
}

From source file:de.devbliss.apitester.factory.impl.EntityBuilderTest.java

@Test
public void buildEntityFromObjectPayload() throws Exception {
    // prepare payload:
    SpittingCobra payload = new SpittingCobra();
    final String name = "Bob";
    payload.setName(name);//from  ww w  .jav a 2 s . c  o  m

    // build entity:
    StringEntity result = entityBuilder.buildEntity(payload);
    assertEquals("application/json", result.getContentType().getValue());
    assertEquals("{\"name\":\"" + name + "\"}", IOUtils.toString(result.getContent()));
}

From source file:org.imsglobal.caliper.request.ApacheHttpRequestorSingleMinimalEventTest.java

@Test
public void testGeneratePayloadContentType() throws Exception {
    // Set up Mapper; filter out nulls/empties
    ObjectMapper mapper = JsonObjectMapper.create(JsonInclude.Include.NON_EMPTY);

    // Serialize envelope; include null properties, empty objects and empty arrays
    String json = httpRequestor.serializeEnvelope(envelope, mapper);

    // Create an HTTP StringEntity payload with the envelope JSON.
    StringEntity payload = httpRequestor.generatePayload(json, ContentType.APPLICATION_JSON);

    assertEquals("Content-Type: application/json; charset=UTF-8", payload.getContentType().toString());
}

From source file:com.nominanuda.springsoy.SoySourceTest.java

@Test
public void testJs() throws Exception {
    SoyJsTemplateServer soyJsTemplateServer = new SoyJsTemplateServer();
    soyJsTemplateServer.setSoySource(soySource);
    HttpRequest req = new HttpGet("/somepath/foo.soy.js?lang=en");
    DataObjectURISpec spec = new DataObjectURISpec("/somepath/{tpl **.soy.js}?{lang en|it}");
    DataObject cmd = spec.match(req.getRequestLine().getUri());
    CommandRequestHandlerAdapter adapter = new CommandRequestHandlerAdapter();
    StringEntity se = (StringEntity) adapter.invoke(soyJsTemplateServer, req, cmd);
    String jsFile = new IOHelper().readAndClose(se.getContent(), HttpProtocol.CS_UTF_8);
    Assert.assertTrue(jsFile.contains("examples.simple.helloWorld2 = function"));
    Assert.assertEquals(HttpProtocol.CT_TEXT_JAVASCRIPT_CS_UTF8, se.getContentType().getValue());
}

From source file:org.imsglobal.caliper.request.ApacheHttpRequestorSingleEventTest.java

@Test
public void testGeneratePayloadContentType() throws Exception {
    // Set up Mapper
    ObjectMapper mapper = JsonObjectMapper.create(JsonInclude.Include.ALWAYS);

    // Serialize envelope
    String json = httpRequestor.serializeEnvelope(envelope, mapper);

    // Create an HTTP StringEntity payload with the envelope JSON.
    StringEntity payload = httpRequestor.generatePayload(json, ContentType.APPLICATION_JSON);

    //System.out.println("CONTENT-TYPE:" + payload.getContentType().toString());

    assertEquals("Content-Type: application/json; charset=UTF-8", payload.getContentType().toString());
}

From source file:impalapayapis.ApiRequestBank.java

public String doPost() {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    StringEntity entity;
    String out = "";

    try {//  w w w. ja v a 2s.  c  o m
        entity = new StringEntity(params);
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(entity);
        HttpResponse response = httpclient.execute(httppost);

        // for debugging
        System.out.println(entity.getContentType());
        System.out.println(entity.getContentLength());
        System.out.println(EntityUtils.toString(entity));
        System.out.println(EntityUtils.toByteArray(entity).length);

        //System.out.println(           "----------------------------------------");

        System.out.println(response.getStatusLine());
        System.out.println(url);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        out = rd.readLine();
        JsonElement root = new JsonParser().parse(out);

        String specificvalue = root.getAsJsonObject().get("replace with key of the value to retrieve")
                .getAsString();
        System.out.println(specificvalue);

        /**
         * String line = ""; while ((line = rd.readLine()) != null) {
         * //System.out.println(line); }
        *
         */

    } catch (UnsupportedEncodingException e) {
        logger.error("UnsupportedEncodingException for URL: '" + url + "'");

        logger.error(ExceptionUtils.getStackTrace(e));
    } catch (ClientProtocolException e) {
        logger.error("ClientProtocolException for URL: '" + url + "'");
        logger.error(ExceptionUtils.getStackTrace(e));
    } catch (IOException e) {
        logger.error("IOException for URL: '" + url + "'");
        logger.error(ExceptionUtils.getStackTrace(e));
    }
    return out;
}