List of usage examples for org.apache.http.entity StringEntity StringEntity
public StringEntity(String str) throws UnsupportedEncodingException
From source file:de.bytefish.fcmjava.client.utils.HttpUtils.java
private static <TRequestMessage, TResponseMessage> TResponseMessage internalPost( HttpClientBuilder httpClientBuilder, IFcmClientSettings settings, TRequestMessage requestMessage, Class<TResponseMessage> responseType) throws Exception { try (CloseableHttpClient client = httpClientBuilder.build()) { // Initialize a new post Request: HttpPost httpPost = new HttpPost(settings.getFcmUrl()); // Get the JSON representation of the given request message: String requestJson = JsonUtils.getAsJsonString(requestMessage); // Set the JSON String as data: httpPost.setEntity(new StringEntity(requestJson)); // Execute the Request: try (CloseableHttpResponse response = client.execute(httpPost)) { // Get the HttpEntity of the Response: HttpEntity entity = response.getEntity(); // If we don't have a HttpEntity, we won't be able to convert it: if (entity == null) { // Simply return null (no response) in this case: return null; }/*from w w w.j ava 2 s .com*/ // Get the JSON Body: String responseBody = EntityUtils.toString(entity); // Make Sure it is fully consumed: EntityUtils.consume(entity); // And finally return the Response Message: return JsonUtils.getEntityFromString(responseBody, responseType); } } }
From source file:com.android.unit_tests.GoogleHttpClientTest.java
protected void setUp() throws Exception { // Run a test server that echoes the URI back to the caller. mServer = new TestHttpServer(); mServer.registerHandler("*", new HttpRequestHandler() { public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { String uri = request.getRequestLine().getUri(); response.setEntity(new StringEntity(uri)); }// www . j a v a2 s. co m }); mServer.start(); mServerUrl = "http://localhost:" + mServer.getPort() + "/"; }
From source file:org.mule.module.http.functional.listener.HttpListenerPathRoutingTestCase.java
@Test public void callPath() throws Exception { final String url = String.format("http://localhost:%s/%s", listenPort.getNumber(), testPath); final Response response = Request.Post(url).body(new StringEntity(testPath)).connectTimeout(1000).execute(); assertThat(response.returnContent().asString(), is(testPath)); }
From source file:es.ucm.look.data.remote.restful.RestMethod.java
/** * To Update an element//from w ww .j a v a 2 s. com * * @param url * Element URI * @param c * The element to update represented with a JSON * @return * The response */ public static HttpResponse doPut(String url, JSONObject c) { HttpClient httpclient = new DefaultHttpClient(); HttpPut request = new HttpPut(url); StringEntity s = null; try { s = new StringEntity(c.toString()); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } s.setContentEncoding("UTF-8"); s.setContentType("application/json"); request.setEntity(s); request.addHeader("accept", "application/json"); try { return httpclient.execute(request); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:com.flipkart.foxtrot.client.TestSelector.java
@Before public void setup() throws Exception { localTestServer.register("/foxtrot/v1/cluster/members", new HttpRequestHandler() { @Override//w w w . j av a2s . c o m public void handle(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { response.setEntity(new StringEntity(mapper.writeValueAsString( new FoxtrotClusterStatus(Lists.newArrayList(new FoxtrotClusterMember("host1", 18000), new FoxtrotClusterMember("host2", 18000)))))); response.setStatusCode(200); } }); localTestServer.start(); }
From source file:org.quizpoll.net.UrlShortenerHelper.java
@Override public HttpUriRequest createRequest() { try {/*from w w w.j a va 2 s . com*/ Uri url = Uri.parse(URL_SHORTENER_URL).buildUpon().appendQueryParameter("key", URL_SHORTENER_API_KEY) .build(); HttpUriRequest request = new HttpPost(url.toString()); String requestString = "{\"longUrl\": \"" + (String) requestData + "\"}"; ((HttpPost) request).setEntity(new StringEntity(requestString)); request.setHeader("Content-Type", "application/json"); return request; } catch (UnsupportedEncodingException e) { return null; } }
From source file:es.uned.dia.jcsombria.model_elements.softwarelinks.nodejs.HttpTransport.java
public Object send(String request) throws Exception { Object response = null;//www . ja v a 2 s.co m CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost(serverURL.toString()); System.out.println("Executing request " + httppost.getRequestLine()); System.out.println(request); httppost.setEntity(new StringEntity(request)); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httppost, responseHandler); response = responseBody; } finally { httpclient.close(); } return response; }
From source file:com.flipkart.foxtrot.client.TestSelectorNoMember.java
@Before public void setup() throws Exception { localTestServer.register("/foxtrot/v1/cluster/members", new HttpRequestHandler() { @Override/* w ww . ja v a 2 s .com*/ public void handle(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { response.setEntity(new StringEntity(mapper .writeValueAsString(new FoxtrotClusterStatus(Lists.<FoxtrotClusterMember>newArrayList())))); response.setStatusCode(200); } }); localTestServer.start(); }
From source file:rc.championship.platform.decoder.lap.publisher.RccLapPublisher.java
private void send(List<Lap> laps) { try {// w w w .jav a2s . c om String json = convertToJsonArray(laps); HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost("http://localhost:8080/api/laps"); StringEntity input = new StringEntity(json); post.setEntity(input); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.talend.dataprep.api.service.command.folder.RenameFolder.java
private HttpRequestBase onExecute(final String id, final String newName) { try {/*from www . ja va 2s. co m*/ final URIBuilder uriBuilder = new URIBuilder(preparationServiceUrl + "/folders/" + id + "/name"); uriBuilder.addParameter("newName", newName); final HttpPut put = new HttpPut(uriBuilder.build()); put.setEntity(new StringEntity(newName)); return put; } catch (UnsupportedEncodingException | URISyntaxException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } }