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) throws UnsupportedEncodingException 

Source Link

Usage

From source file:HelloWorld.HelloWorldTest.java

@Test
public void postPeopleTest() throws URISyntaxException, IOException {
    URI uri = new URI(baseURI + "api/person");
    HttpPost post = new HttpPost(uri);
    Person person = new Person();
    person.firstName = "Sally";
    person.lastName = "Smith";
    person.age = 25;// w  w w . ja  va 2s.  c o  m
    person.married = false;
    person.birthDate = new Date();

    Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy").create();
    String json = gson.toJson(person);
    post.setEntity(new StringEntity(json));

    CloseableHttpResponse response = client.execute(post);
    assertEquals(201, response.getStatusLine().getStatusCode());
    assertEquals("Person creation success!", EntityUtils.toString(response.getEntity(), "UTF-8"));
}

From source file:org.apache.camel.component.http4.HttpConcurrentTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/", new HttpRequestHandler() {
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // ignore
            }/*w w w  .  j  av  a  2 s. c om*/
            response.setStatusCode(HttpStatus.SC_OK);
            response.setEntity(new StringEntity("" + counter.incrementAndGet()));
        }
    });
}

From source file:fakingXmocking.CurrencyConversionHttpClientFake.java

private HttpEntity createHttpResponse(String content) {
    try {/*from w w w. j  av  a2  s  .  co m*/
        return new StringEntity(content);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:PostTest.PostTest.java

public String post(String apiURL, String parameters) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost method = new HttpPost(apiURL);
    String body = null;//  w  w  w .j  a v  a2 s.  c om

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

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

            HttpResponse response = httpclient.execute(method);

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

        } catch (IOException e) {

        } finally {
        }

    }
    return body;
}

From source file:com.klarna.checkout.HandlerTest.java

/**
 * Test of handleResponse with an invalid json response.
 *
 * @throws IOException if things go as planned.
 *//*  ww w .  jav a2 s .co  m*/
@Test(expected = IOException.class)
public void testHandleBadResponse() throws IOException {
    HttpResponse res = mock(HttpResponse.class);
    Handler handler = new Handler(mock(IResource.class));
    StatusLine sline = mock(StatusLine.class);
    when(sline.getStatusCode()).thenReturn(200);
    when(res.getStatusLine()).thenReturn(sline);
    when(res.getEntity()).thenReturn(new StringEntity(""));
    handler.handleResponse(res);
}

From source file:com.dv.sharer.android.rest.RestHttpOperations.java

protected HttpPost getPOST(String urlStr, String data) throws IOException {
    HttpPost httppost = new HttpPost(urlStr);
    httppost.setHeader("Content-Type", "application/json");
    StringEntity entity = new StringEntity(data);
    entity.setContentType("application/json");
    httppost.addHeader("Accept", "application/json");
    httppost.setEntity(entity);/*from   www.j a  va2  s  . co m*/
    return httppost;
}

From source file:ly.apps.androdi.rest.tests.JsonBodyConverterTest.java

public void testFromResponseBody() throws UnsupportedEncodingException {
    assertEquals(SampleBean.testInstance(),
            converter.fromResponseBody(SampleBean.class, HeaderUtils.CONTENT_TYPE_JSON,
                    new StringEntity(SampleBean.testInstanceAsString()), new TestCallback<SampleBean>()));
}

From source file:net.modelbased.proasense.storage.registry.RestRequest.java

public static String postSensor(Sensor sensor) {
    String content = JsonPrinter.sensorToJson(sensor);
    URI target = null;/* w w  w.  j a v  a 2  s  .com*/
    try {
        target = new URI(sensor.getUri().toString() + SENSOR_PATH);
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
    }
    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost(target);
    request.setHeader("Content-type", "application/json");
    String response = null;
    try {
        StringEntity seContent = new StringEntity(content);
        seContent.setContentType("text/json");
        request.setEntity(seContent);
        response = resolveResponse(client.execute(request));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

From source file:se.vgregion.pubsub.inttest.Publisher.java

public Publisher(String host) throws Exception {
    this.localServerName = host;
    server = new LocalTestServer(null, null);
    server.register("/*", new HttpRequestHandler() {
        @Override/*from w  ww . j a  v a2  s  .c  o  m*/
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setHeader("Content-type", ContentType.ATOM.toString());
            response.setEntity(new StringEntity(AbstractSerializer.printFeed(ContentType.ATOM, feed).toXML()));
        }
    });
    server.start();
}

From source file:ca.sqlpower.dao.json.JSONHttpMessageSender.java

public void flush() throws SPPersistenceException {
    try {/*from   ww w . ja v  a  2  s. c  o  m*/
        URI serverURI = getServerURI();
        HttpPost postRequest = new HttpPost(serverURI);
        postRequest.setEntity(new StringEntity(messageArray.toString()));
        postRequest.setHeader("Content-Type", "application/json");
        HttpUriRequest request = postRequest;
        getHttpClient().execute(request, new ResponseHandler<Void>() {
            public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                StatusLine statusLine = response.getStatusLine();
                if (statusLine.getStatusCode() >= 400) {
                    throw new ClientProtocolException("HTTP Post request returned an error: " + "Code = "
                            + statusLine.getStatusCode() + ", " + "Reason = " + statusLine.getReasonPhrase());
                }
                return null;
            }
        });
    } catch (URISyntaxException e) {
        throw new SPPersistenceException(null, e);
    } catch (ClientProtocolException e) {
        throw new SPPersistenceException(null, e);
    } catch (IOException e) {
        throw new SPPersistenceException(null, e);
    } finally {
        clearMessageArray();
    }
}