List of usage examples for org.apache.http.client.methods HttpPost setEntity
public void setEntity(final HttpEntity entity)
From source file:uf.edu.AddLocation.java
public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) { JSONObject obj = null;/*from w w w. j a v a2 s . co m*/ try { DefaultHttpClient httpclient = new DefaultHttpClient(); 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"); HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); // Get hold of the response entity (-> the data): HttpEntity entity = response.getEntity(); if (entity != null) { // Read the content stream InputStream instream = entity.getContent(); // convert content stream to a String String resultString = convertStreamToString(instream); instream.close(); resultString = resultString.substring(1, resultString.length() - 1); // remove wrapping "[" and "]" String s = "{" + resultString;//+"}"; Log.i(TAG, "Location String Received "); obj = (JSONObject) new JSONTokener(s).nextValue(); } else { Log.i(TAG, "AddLocation: error response from the location server :" + response.toString()); } } catch (Exception e) { e.printStackTrace(); } return obj;//jsonObjRecv; }
From source file:com.tedx.webservices.WebServices.java
public static JSONArray SendHttpPostArray(String URL, JSONObject jsonObjSend) { try {// w w w .j a va 2 s.c om DefaultHttpClient httpclient = new DefaultHttpClient(); 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(); 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(); // Transform the String into a JSONObject JSONArray jsonObjRecv = new JSONArray(resultString); // Raw DEBUG output of our received JSON object: Log.i(TAG, "<jsonobject>\n" + jsonObjRecv.toString() + "\n</jsonobject>"); return jsonObjRecv; } } catch (Exception e) { // More about HTTP exception handling in another tutorial. // For now we just print the stack trace. e.printStackTrace(); } return null; }
From source file:com.wbtech.ums.common.NetworkUitlity.java
public static String Post(String url, String data) { CommonUtil.printLog("ums", url); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try {/*from ww w .jav a2 s . c o m*/ StringEntity se = new StringEntity("content=" + data, HTTP.UTF_8); CommonUtil.printLog("postdata", "content=" + data); se.setContentType("application/x-www-form-urlencoded"); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); CommonUtil.printLog("ums", status + ""); String returnXML = EntityUtils.toString(response.getEntity()); Log.d("returnString", URLDecoder.decode(returnXML)); return URLDecoder.decode(returnXML); } catch (Exception e) { CommonUtil.printLog("ums", e.toString()); } return null; }
From source file:com.srotya.tau.ui.BapiLoginDAO.java
public static Entry<String, String> authenticate(String authURL, String username, String password) throws Exception { CloseableHttpClient client = Utils.buildClient(authURL, 3000, 5000); HttpPost authRequest = new HttpPost(authURL); Gson gson = new Gson(); JsonObject obj = new JsonObject(); obj.addProperty(USERNAME, username); obj.addProperty(PASSWORD, password); StringEntity entity = new StringEntity(gson.toJson(obj), ContentType.APPLICATION_JSON); authRequest.setEntity(entity); CloseableHttpResponse response = client.execute(authRequest); if (response.getStatusLine().getStatusCode() == 200) { String tokenPair = EntityUtils.toString(response.getEntity()); JsonArray ary = gson.fromJson(tokenPair, JsonArray.class); obj = ary.get(0).getAsJsonObject(); String token = obj.get(X_SUBJECT_TOKEN).getAsString(); String hmac = obj.get(HMAC).getAsString(); return new AbstractMap.SimpleEntry<String, String>(token, hmac); } else {/*from ww w .ja v a 2 s . c om*/ System.err.println("Login failed:" + response.getStatusLine().getStatusCode() + "\t" + response.getStatusLine().getReasonPhrase()); return null; } }
From source file:org.cloudsimulator.utility.RestAPI.java
private static CloseableHttpResponse postRequestBasicAuth(final CloseableHttpClient httpClient, final String uri, final String username, final String password, final String contentType, final HttpEntity entityToSend) throws IOException { HttpPost httpPost = new HttpPost(uri); httpPost.addHeader(CONTENTTYPE, contentType); httpPost.addHeader(AUTHORIZATION, getBasicAuth(username, password)); httpPost.setEntity(entityToSend); return httpClient.execute(httpPost); }
From source file:org.jitsi.meet.test.util.JvbUtil.java
static private void triggerShutdown(HttpClient client, String jvbEndpoint, boolean force) throws IOException { String url = jvbEndpoint + "/colibri/shutdown"; HttpPost post = new HttpPost(url); StringEntity requestEntity = new StringEntity( force ? "{ \"force-shutdown\": \"true\" }" : "{ \"graceful-shutdown\": \"true\" }", ContentType.APPLICATION_JSON); post.setEntity(requestEntity); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + post.getEntity()); HttpResponse response = client.execute(post); int responseCode = response.getStatusLine().getStatusCode(); if (200 != responseCode) { throw new RuntimeException( "Failed to trigger graceful shutdown on: " + jvbEndpoint + ", response code: " + responseCode); }// w w w . jav a 2 s . com }
From source file:org.wise.vle.domain.webservice.crater.CRaterHttpClient.java
/** * Handles POSTing a CRater Request to the CRater Servlet and returns the * CRater response string.//ww w . j ava2 s. c o m * * @param cRaterUrl the CRater url * @param bodyData the xml body data to be sent to the CRater server * @return the response from the CRater server */ public static String post(String cRaterUrl, String bodyData) { String responseString = null; if (cRaterUrl != null) { HttpClient client = HttpClientBuilder.create().build(); // Create a method instance. HttpPost post = new HttpPost(cRaterUrl); try { //System.out.println("CRater request bodyData:" + bodyData); post.setEntity(new StringEntity(bodyData, ContentType.TEXT_XML)); // Execute the method. HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { System.err.println("Method failed: " + response.getStatusLine()); } // Read the response body. responseString = IOUtils.toString(response.getEntity().getContent()); //System.out.println("responseString: " + responseString); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. post.releaseConnection(); } } return responseString; }
From source file:Pusher.java
/** * Delivers a message to the Pusher API// w ww . j ava 2 s . c om * @param channel * @param event * @param jsonData * @param socketId * @return * @throws IOException * @throws ClientProtocolException */ public static String triggerPush(String channel, String event, String jsonData, String socketId) throws ClientProtocolException, IOException { //Build URI path String uriPath = buildURIPath(channel); //Build query String query = buildQuery(event, jsonData, socketId); //Generate signature String signature = buildAuthenticationSignature(uriPath, query); //Build URI String url = buildURI(uriPath, query, signature); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpContext cntxt = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(jsonData)); org.apache.http.HttpResponse httpResponse = httpClient.execute(httpPost); //Start request try { return EntityUtils.toString(httpResponse.getEntity()); } catch (IOException e) { //Log warning return null; } }
From source file:cn.mrdear.pay.util.WebUtils.java
/** * ?/* ww w . java 2 s . c om*/ * @param certPath ? * @param passwd ?? * @param uri ? * @param entity xml * @return */ public static String post(String certPath, String passwd, String uri, InputStreamEntity entity) throws Exception { String result = null; KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(certPath)); try { keyStore.load(instream, passwd.toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, passwd.toCharArray()).build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { HttpPost httpPost = new HttpPost(uri); entity.setContentEncoding("UTF-8"); httpPost.setEntity(entity); CloseableHttpResponse httpResponse = httpclient.execute(httpPost); result = consumeResponse(httpResponse); } finally { httpclient.close(); } return result; }
From source file:com.pyz.tool.weixintool.util.HttpTool.java
public static String postRequest(String url, String content) throws Exception { String result = ""; HttpClient httpClient = new DefaultHttpClient(); try {/*from ww w . j a v a 2s . c om*/ HttpPost httppost = new HttpPost(url); StringEntity myEntity = new StringEntity(content, "utf-8"); myEntity.setContentEncoding("utf-8"); myEntity.setContentType("application/json"); httppost.setEntity(myEntity); HttpResponse httpresponse = httpClient.execute(httppost); HttpEntity entity = httpresponse.getEntity(); result = EntityUtils.toString(entity); } catch (ParseException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }