List of usage examples for org.apache.http.client.methods HttpPost HttpPost
public HttpPost(final String uri)
From source file:coolmapplugin.util.HTTPRequestUtil.java
public static String executePost(String targetURL, String jsonBody) { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { HttpPost request = new HttpPost(targetURL); StringEntity params = new StringEntity(jsonBody); request.addHeader("content-type", "application/json"); request.setEntity(params);/* w ww. j a v a2 s . c om*/ HttpResponse result = httpClient.execute(request); HttpEntity entity = result.getEntity(); if (entity == null) return null; String jsonResult = EntityUtils.toString(result.getEntity(), "UTF-8"); return jsonResult; } catch (IOException e) { return null; } }
From source file:it410.gmu.edu.OrderingServiceClient.java
public static void addCustomerJSON(Customer customer) throws IOException { Gson gson = new Gson(); String customerString = gson.toJson(customer); System.out.println("Customer JSON = " + customerString); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://localhost:8080/BookstoreRestService/generic/addOrderJSON"); post.setHeader("Content-Type", "application/json"); StringEntity entity = new StringEntity(customerString); post.setEntity(entity);/*from w ww .ja v a2 s. co m*/ 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); } }
From source file:com.fishcart.delivery.util.HttpClient.java
public static String postWhatsapp(String url, String nos, String message) { try {/* w w w . j a v a2 s.c o m*/ CloseableHttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(url); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("nos", nos)); urlParameters.add(new BasicNameValuePair("message", message)); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder result = new StringBuilder(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } return result.toString(); } catch (IOException ex) { throw new RuntimeException(ex); } }
From source file:Technique.PostFile.java
public static void upload(String files) throws Exception { String userHome = System.getProperty("user.home"); HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost("http://localhost/image/up.php"); File file = new File(files); MultipartEntity mpEntity = new MultipartEntity(); ContentBody contentFile = new FileBody(file); mpEntity.addPart("userfile", contentFile); httppost.setEntity(mpEntity);/*from www. j ava 2 s . com*/ System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); if (!(response.getStatusLine().toString()).equals("HTTP/1.1 200 OK")) { // Successfully Uploaded } else { // Did not upload. Add your logic here. Maybe you want to retry. } System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity)); } if (resEntity != null) { resEntity.consumeContent(); } httpclient.getConnectionManager().shutdown(); }
From source file:costumetrade.common.http.HttpClientUtilsWrapper.java
public static HttpResponse doPost(HttpMessage message) throws UnsupportedOperationException, IOException { logger.info("?{}....", message.getUrl()); try (CloseableHttpClient httpclient = HttpClients.custom().build();) { HttpPost request = new HttpPost(message.getUrl()); request.setEntity(message.getHttpEntity()); try (CloseableHttpResponse response = httpclient.execute(request);) { int statusCode = response.getStatusLine().getStatusCode(); logger.info("?{}???{}", message.getUrl(), statusCode); if (HttpStatus.SC_OK == statusCode) { String result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8.name()); logger.info("?:{}", result); return new HttpResponse(statusCode, result, message.getCallbackData()); }/* ww w.j a v a 2s . c o m*/ return new HttpResponse(statusCode, null, message.getCallbackData()); } } }
From source file:Main.java
public static final String postData(String url, HashMap<String, String> params) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); String outputString = null;//from w w w. j ava 2s .co m try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size()); Iterator it = params.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); nameValuePairs.add(new BasicNameValuePair((String) pair.getKey(), (String) pair.getValue())); System.out.println(pair.getKey() + " = " + pair.getValue()); it.remove(); // avoids a ConcurrentModificationException } httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) { byte[] result = EntityUtils.toByteArray(response.getEntity()); outputString = new String(result, "UTF-8"); } } catch (ClientProtocolException e) { Log.i(CLASS_NAME, "Client protocolException happened: " + e.getMessage()); } catch (IOException e) { Log.i(CLASS_NAME, "Client IOException happened: " + e.getMessage()); } catch (NetworkOnMainThreadException e) { Log.i(CLASS_NAME, "Client NetworkOnMainThreadException happened: " + e.getMessage()); e.printStackTrace(); } catch (Exception e) { Log.i(CLASS_NAME, "Unknown exeception: " + e.getMessage()); } return outputString; }
From source file:cardgametrackercs319.DBConnectionManager.java
public static String saveTrackScores(TrackEngine engine) throws UnsupportedEncodingException, IOException { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://ozymaxx.net/cs319/save_tracks_scores.php"); post.setHeader("User-Agent", USER_AGENT); ArrayList<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("info", engine.getJSON())); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String resLine = ""; while ((resLine = reader.readLine()) != null) { result.append(resLine);//w ww.j a va2s. c o m } return result.toString(); }
From source file:core.VirusTotalAPIHelper.java
public static CloseableHttpResponse scanFile(File fileToScan) { if (apiKey == null || apiKey.isEmpty()) { return null; }/*from ww w . ja va2s . co m*/ try { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpsScanFilePost = new HttpPost(httpsPost + "file/scan"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("apikey", apiKey); builder.addBinaryBody("file", fileToScan, ContentType.APPLICATION_OCTET_STREAM, fileToScan.getName()); HttpEntity multipart = builder.build(); httpsScanFilePost.setEntity(multipart); CloseableHttpResponse response = client.execute(httpsScanFilePost); return response; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return null; }
From source file:de.randec.MVBMonitor.Downloader.java
static String downloadJson(String url) throws IOException { String data = ""; try {//from w w w.java 2 s . c o m // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost); data = EntityUtils.toString(response.getEntity(), "utf-8"); //data = EntityUtils.toString(response.getEntity()); } catch (ClientProtocolException e) { e.printStackTrace(); } //unescape escape codes (Umlaute) return StringEscapeUtils.unescapeHtml4(data); }
From source file:wvw.utils.pc.HttpHelper.java
public static String post(String url, JSONArray headers, String data) { HttpPost httpPost = new HttpPost(url); for (int i = 0; i < headers.length(); i++) { JSONArray header = headers.getJSONArray(i); httpPost.addHeader(header.getString(0), header.getString(1)); }/*from w w w . j a v a 2 s. c o m*/ String ret = null; try { httpPost.setEntity(new StringEntity(data)); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpPost); StatusLine status = response.getStatusLine(); switch (status.getStatusCode()) { case 200: ret = IOUtils.readFromStream(response.getEntity().getContent()); break; default: ret = genError(status.getReasonPhrase()); break; } } catch (IOException e) { // e.printStackTrace(); ret = genError(e.getClass() + ": " + e.getMessage()); } return ret; }