List of usage examples for org.apache.http.client.methods HttpPost HttpPost
public HttpPost(final String uri)
From source file:com.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java
public static String sendPost(String serverURL, String postBody) throws Exception { CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(serverURL); StringEntity entity = new StringEntity(postBody, "UTF-8"); post.setEntity(entity);/*ww w .ja v a2s . c o m*/ HttpResponse response = client.execute(post); // System.out.println("\nSending 'POST' request to URL : " + serverURL); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } client.close(); // System.out.println(result.toString()); return result.toString(); }
From source file:pingdesktop.Model.HttpHandler.java
public static String doPost(List<NameValuePair> nvps, String target_url) throws IOException { /* create the HTTP client and POST request */ HttpPost httpPost = new HttpPost(BaseUrl + target_url); /* add some request parameters for a form request */ // List<NameValuePair> nvps = new ArrayList<NameValuePair>(); ////from www .ja v a 2 s. c o m // nvps.add(new BasicNameValuePair("username", "pangeranweb")); // nvps.add(new BasicNameValuePair("password", "anisnuzulan")); nvps.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded")); httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); /* execute request */ HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); /* process response */ if (httpResponse.getStatusLine().getStatusCode() == 200) { String responseText = EntityUtils.toString(httpEntity); return responseText; } else { return "Invalid HTTP response: " + httpResponse.getStatusLine().getStatusCode(); } }
From source file:Main.java
public static void PostRequest(String url, Map<String, String> params, String userName, String password, Handler messageHandler) { HttpPost postMethod = new HttpPost(url); List<NameValuePair> nvps = null; DefaultHttpClient client = new DefaultHttpClient(); if ((userName != null) && (userName.length() > 0) && (password != null) && (password.length() > 0)) { client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); }//from w w w .ja v a2s . com final Map<String, String> sendHeaders = new HashMap<String, String>(); sendHeaders.put(CONTENT_TYPE, MIME_FORM_ENCODED); client.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { for (String key : sendHeaders.keySet()) { if (!request.containsHeader(key)) { request.addHeader(key, sendHeaders.get(key)); } } } }); if ((params != null) && (params.size() > 0)) { nvps = new ArrayList<NameValuePair>(); for (String key : params.keySet()) { nvps.add(new BasicNameValuePair(key, params.get(key))); } } if (nvps != null) { try { postMethod.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } ExecutePostRequest(client, postMethod, GetResponseHandlerInstance(messageHandler)); }
From source file:org.ojbc.util.helper.HttpUtils.java
/** * Send the specified payload to the specified http endpoint via POST. * @param payload//www . j a v a 2 s.c o m * @param url * @return the http response * @throws Exception */ public static String post(String payload, String url) throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.setEntity(new StringEntity(payload, Consts.UTF_8)); HttpResponse response = client.execute(post); HttpEntity reply = response.getEntity(); StringWriter sw = new StringWriter(); BufferedReader isr = new BufferedReader(new InputStreamReader(reply.getContent())); String line; while ((line = isr.readLine()) != null) { sw.append(line); } sw.close(); return sw.toString(); }
From source file:com.cbtec.eliademyutils.EliademyUtils.java
public static String serviceCall(String data, String webService, String token, String serviceClient) { String retval = null;/*from w ww .ja va 2 s . c om*/ Log.d("EliademyUtils", "Service: " + data + " Service: " + webService); try { DefaultHttpClient httpclient = new DefaultHttpClient(); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); if (data.toString().length() > 0) { JSONObject jsObj = new JSONObject(data.toString()); nameValuePairs.addAll(nameValueJson(jsObj, "")); } nameValuePairs.add(new BasicNameValuePair("wstoken", token)); nameValuePairs.add(new BasicNameValuePair("wsfunction", webService)); nameValuePairs.add(new BasicNameValuePair("moodlewsrestformat", "json")); HttpPost httppost = new HttpPost(serviceClient + "/webservice/rest/server.php?"); Log.d("EliademyUtils", nameValuePairs.toString()); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { StringBuilder jsonStr = new StringBuilder(); InputStream iStream = entity.getContent(); BufferedReader bufferReader = new BufferedReader(new InputStreamReader(iStream)); String jpart; while ((jpart = bufferReader.readLine()) != null) { jsonStr.append(jpart); } iStream.close(); return jsonStr.toString(); } } catch (Exception e) { Log.e("EliademyUtils", "exception", e); return retval; } return retval; }
From source file:de.hybris.platform.integration.cis.payment.strategies.impl.CisPaymentIntegrationTestHelper.java
public static Map<String, String> createNewProfile(final String cybersourceUrl, final List<BasicNameValuePair> formData) throws Exception // NO PMD { final DefaultHttpClient client = new DefaultHttpClient(); final HttpPost postRequest = new HttpPost(cybersourceUrl); postRequest.getParams().setBooleanParameter("http.protocol.handle-redirects", true); postRequest.setEntity(new UrlEncodedFormEntity(formData, "UTF-8")); // Execute HTTP Post Request final HttpResponse response = client.execute(postRequest); final Map<String, String> responseParams = new HashMap<String, String>(); BufferedReader bufferedReader = null; try {/*from w ww . ja v a 2 s.co m*/ bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); while (bufferedReader.ready()) { final String currentLine = bufferedReader.readLine(); if (currentLine.contains("value=\"") && currentLine.contains("name=\"")) { final String[] splittedLine = currentLine.split("name=\""); final String key = splittedLine[1].split("value=\"")[0].replace("\"", "").replace(">", "") .trim(); final String value = splittedLine[1].split("value=\"")[1].replace("\"", "").replace(">", "") .trim(); responseParams.put(key, value); } } } finally { IOUtils.closeQuietly(bufferedReader); } return responseParams; }
From source file:org.openo.nfvo.monitor.umc.monitor.common.HttpClientUtil.java
/** * @Title httpPost/*from www .jav a 2 s . co m*/ * @Description TODO(realize the rest interface to access by httpPost) * @param url * @throws Exception * @return int (return StatusCode,If zero error said) */ public static int httpPost(String url) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); try { CloseableHttpResponse res = httpClient.execute(httpPost); try { return res.getStatusLine().getStatusCode(); } finally { res.close(); } } catch (ParseException e) { LOGGER.error("HttpClient throw ParseException:" + url, e); } catch (IOException e) { LOGGER.error("HttpClient throw IOException:" + url, e); } finally { try { httpClient.close(); } catch (IOException e) { // TODO Auto-generated catch block LOGGER.error("HttpClient Close throw IOException", e); } } return 0; }
From source file:de.bytefish.fcmjava.client.utils.HttpUtils.java
private static <TRequestMessage> void internalPost(HttpClientBuilder httpClientBuilder, IFcmClientSettings settings, TRequestMessage requestMessage) throws Exception { try (CloseableHttpClient client = httpClientBuilder.build()) { // Initialize a new post Request: HttpPost httpPost = new HttpPost(settings.getFcmUrl()); // Set the JSON String as data: httpPost.setEntity(new StringEntity(JsonUtils.getAsJsonString(requestMessage))); // Execute the Request: try (CloseableHttpResponse response = client.execute(httpPost)) { // Get the HttpEntity: HttpEntity entity = response.getEntity(); // Let's be a good citizen and consume the HttpEntity: if (entity != null) { // Make Sure it is fully consumed: EntityUtils.consume(entity); }/*from w ww .j a v a2 s.c o m*/ } } }
From source file:com.zhongsou.souyue.service.SelfCreateUploadHttp.java
public static HttpJsonResponse doPost(String url, Map<String, String> params) { if (!CMainHttp.getInstance().isNetworkAvailable(MainApplication.getInstance())) return null; try {//w w w.j ava 2 s . c om HttpPost post = new HttpPost(url); // List<NameValuePair> pairs = new ArrayList<NameValuePair>(); // String value = null; // // for (Map.Entry<String, String> e : params.entrySet()) { // value = e.getValue(); // if (value != null) { // pairs.add(new BasicNameValuePair(e.getKey(), value.toString())); // } // } post.addHeader("User-Agent", AGENT); List<BasicNameValuePair> pairs = Utils.encryptToPostEntity(url, params, null); post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8)); HttpResponse resp = new DefaultHttpClient().execute(post); if (resp.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(resp.getEntity()); HttpJsonResponse json = new HttpJsonResponse((JsonObject) new JsonParser().parse(result)); return json; } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.spokenpic.net.RestClientFilePost.java
@Override protected RestResult doPost() { HttpPost httpPost = new HttpPost(getSchemeServer() + this.uri); setupHttp(httpPost);/*w ww . ja v a2s . co m*/ try { FileEntity e = new FileEntity(new File(data), mime); httpPost.setEntity(e); HttpResponse httpResponse = HttpManager.execute(httpPost); return returnResponse(httpResponse); } catch (Exception e) { Log.d("RestClientFilePost", "Error doPost " + e.toString()); return errorResponse("Fatal error during file POST"); } }