List of usage examples for org.apache.http.entity StringEntity StringEntity
public StringEntity(String str) throws UnsupportedEncodingException
From source file:es.ugr.swad.swadroid.webservices.RestEasy.java
public static HttpResponse doPut(String url, JSONObject c) throws ClientProtocolException, IOException { HttpClient httpclient = new DefaultHttpClient(); HttpPut request = new HttpPut(url); StringEntity s = new StringEntity(c.toString()); s.setContentEncoding("UTF-8"); s.setContentType("application/json"); request.setEntity(s);/* w w w . j av a2s .com*/ request.addHeader("accept", "application/json"); return httpclient.execute(request); }
From source file:com.arpnetworking.kairosdb.KairosHelper.java
/** * Creates the JSON body for a datapoint POST. * * @param timestamp timestamp of the datapoint * @param histogram the histogram/* w w w .j a va 2 s.co m*/ * @param metricName the name of the metric * @return JSON Array to POST to KairosDB * @throws JSONException on a JSON error */ public static HttpPost postHistogram(final int timestamp, final Histogram histogram, final String metricName) throws JSONException { final JSONArray json = new JSONArray(); final JSONObject metric = new JSONObject(); final JSONArray datapoints = new JSONArray(); final JSONArray datapoint = new JSONArray(); final JSONObject tags = new JSONObject(); tags.put("host", "foo_host"); datapoint.put(timestamp); datapoint.put(histogram.getJson()); datapoints.put(datapoint); metric.put("name", metricName).put("ttl", 600).put("type", "histogram").put("datapoints", datapoints) .put("tags", tags); json.put(metric); final HttpPost post = new HttpPost(KairosHelper.getEndpoint() + "/api/v1/datapoints"); try { post.setEntity(new StringEntity(json.toString())); } catch (final UnsupportedEncodingException e) { throw new RuntimeException(e); } post.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); return post; }
From source file:tech.beshu.ror.integration.DynamicVariablesTests.java
private static void insertDoc(String indexName, RestClient restClient) { if (adminClient == null) { adminClient = restClient;//w ww .jav a 2 s.com } String docPath = "/" + indexName + "/documents/doc-asd"; try { HttpPut request = new HttpPut(restClient.from(docPath) + "?refresh=true"); request.setHeader("Content-Type", "application/json"); request.setHeader("refresh", "true"); request.setHeader("timeout", "50s"); request.setEntity(new StringEntity("{\"title\": \"" + indexName + "\"}")); System.out.println(body(restClient.execute(request))); } catch (Exception e) { e.printStackTrace(); throw new IllegalStateException("Test problem", e); } }
From source file:fossasia.valentina.bodyapp.sync.Sync.java
/** * Method which makes all the POST calls * /*from ww w. ja v a2 s . c om*/ * @param url * @param json * @return */ public String POST(String url, String json, int conTimeOut, int socTimeOut) { InputStream inputStream = null; String result = ""; try { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, conTimeOut); HttpConnectionParams.setSoTimeout(httpParameters, socTimeOut); HttpClient httpclient = new DefaultHttpClient(httpParameters); HttpPost httpPost = new HttpPost(url); StringEntity se = new StringEntity(json); httpPost.setEntity(se); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); HttpResponse httpResponse = httpclient.execute(httpPost); inputStream = httpResponse.getEntity().getContent(); if (inputStream != null) result = convertInputStreamToString(inputStream); else result = "Did not work!"; System.out.println(result + "result"); } catch (Exception e) { Log.d("InputStream", e.getLocalizedMessage()); } return result; }
From source file:com.oracle.jes.samples.hellostorage.SendToServlet.java
public String sendRecord(String data, String srvName) { mode = false;//from w w w . jav a 2 s . c om String out = null; // DefaultHttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = HttpClientBuilder.create().build(); HttpPost httppostreq; if (!mode) { httppostreq = new HttpPost("http://192.168.1.5:8080/pichrony/" + srvName); } else { httppostreq = new HttpPost("http://security.netmaxjava.com/phlogin"); } StringEntity se = null; try { se = new StringEntity(data); } catch (UnsupportedEncodingException ex) { //Logger.getLogger(RegisterDevice.class.getName()).log(Level.SEVERE, null, ex); } // se.setContentType("application/json;"); httppostreq.setEntity(se); try { HttpResponse respo = httpclient.execute(httppostreq); if (respo != null) { out = ""; InputStream inputstream = respo.getEntity().getContent(); out = convertStreamToString(inputstream); } else { } } catch (ClientProtocolException e) { } catch (IOException | IllegalStateException e) { } return out; }
From source file:vic.collaborativeClouds.serverWorkers.HttpPostWorker.java
public String LoginPostRequest(String postUrl, String postJSONData) throws IOException, JSONException { try {/*from w ww . jav a 2 s.c om*/ HttpPost post = new HttpPost(postUrl); HttpClient httpClient = new DefaultHttpClient(); StringEntity postingString = new StringEntity(postJSONData); post.setEntity(postingString); post.setHeader("Content-type", "application/json"); HttpResponse response = httpClient.execute(post); String server = response.getFirstHeader("x-session").getValue(); System.err.println(response.getFirstHeader("status").getValue()); System.err.println(server); SessionStore.session_id = server; return response.getFirstHeader("status").getValue(); } catch (UnsupportedEncodingException ex) { return "Failed"; //Logger.getLogger(HttpPostWorker.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.wso2.carbon.tfl.realtime.TflStream.java
public static void send(ArrayList<String> jsonList, String endPoint) { for (String data : jsonList) { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(endPoint); try {// w w w .ja v a 2s . co m StringEntity entity = new StringEntity(data); post.setEntity(entity); HttpResponse response = client.execute(post); log.info("data sent : " + data); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.bluehermit.apps.module.soap.client.HttpClientHelperImpl.java
public String post(String target, String message) throws Exception { String responeMessage = null; CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w w w .j a v a 2s . c om HttpPost httppost = new HttpPost(target); httppost.setHeader("Content-Type", "text/xml"); httppost.setEntity(new StringEntity(message)); System.out.println("Executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to bother about connection release if (entity != null) { InputStream instream = entity.getContent(); try { responeMessage = IOUtils.toString(instream, "UTF-8"); // do something useful with the response } catch (IOException ex) { // In case of an IOException the connection will be released // back to the connection manager automatically throw ex; } finally { // Closing the input stream will trigger connection release instream.close(); } } } finally { response.close(); } } finally { httpclient.close(); } return responeMessage; }
From source file:com.avinashbehera.sabera.network.HttpClient.java
public static JSONObject SendHttpPostUsingHttpClient(String URL, JSONObject jsonObjSend) { try {//from ww w . ja v a 2s .co m HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 60 * 1000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 60 * 1000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters); HttpPost httpPostRequest = new HttpPost(URL); StringEntity se; se = new StringEntity(jsonObjSend.toString()); // Set HTTP parameters httpPostRequest.setEntity(se); //httpPostRequest.setHeader("Accept", "application/json"); httpPostRequest.setHeader("Content-type", "application/json"); //httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression long t = System.currentTimeMillis(); Log.d(TAG, "httpPostReuest = " + httpPostRequest.toString()); HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]"); // Get hold of the response entity (-> the data): HttpEntity entity = response.getEntity(); if (entity != null) { // Read the content stream InputStream instream = entity.getContent(); Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { instream = new GZIPInputStream(instream); } // convert content stream to a String String resultString = convertStreamToString(instream); instream.close(); resultString = resultString.substring(1, resultString.length() - 1); // remove wrapping "[" and "]" // Transform the String into a JSONObject JSONParser parser = new JSONParser(); JSONObject jsonObjRecv = (JSONObject) parser.parse(resultString); // Raw DEBUG output of our received JSON object: Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>"); return jsonObjRecv; } } catch (Exception e) { Log.d(TAG, "catch block"); // More about HTTP exception handling in another tutorial. // For now we just print the stack trace. e.printStackTrace(); } Log.d(TAG, "SendHttpPostUsingHttpClient returning null"); return null; }
From source file:com.ericsson.gerrit.plugins.highavailability.forwarder.rest.HttpResponseHandlerTest.java
private static HttpResponse setupMocks(int httpCode, String entity) throws UnsupportedEncodingException { StatusLine status = mock(StatusLine.class); when(status.getStatusCode()).thenReturn(httpCode); HttpResponse response = mock(HttpResponse.class); when(response.getStatusLine()).thenReturn(status); when(response.getEntity()).thenReturn(new StringEntity(entity)); return response; }