List of usage examples for org.apache.http.client.methods HttpPost HttpPost
public HttpPost(final String uri)
From source file:com.mysoft.b2b.event.util.HttpUtil.java
/** * http???/* w w w . j a v a 2 s . com*/ * @param uri ? * @param body ? */ public static Integer send(String uri, String body) { if (StringUtils.isEmpty(uri) || StringUtils.isEmpty(body)) return null; HttpClient client = new DefaultHttpClient(); try { HttpPost post = new HttpPost(uri + "/dealEvent.do"); post.addHeader("Content-Type", "application/json;charset=UTF-8"); HttpEntity entity = new StringEntity(body); post.setEntity(entity); HttpResponse response = client.execute(post); int code = response.getStatusLine().getStatusCode(); return code; } catch (Exception e) { logger.error("http???" + e); } return null; }
From source file:com.zhaosen.util.HttpClientUtil.java
License:asdf
@SuppressWarnings({ "rawtypes", "unchecked" }) public static String httpPost(String url, String param) throws ClientProtocolException, IOException { new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); ArrayList params = new ArrayList(); params.add(new BasicNameValuePair("data", param)); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); HttpResponse response = (new DefaultHttpClient()).execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(response.getEntity()); return result; } else {// w w w .j av a2s . co m return ""; } }
From source file:Main.java
/** * Op Http post request , "404error" response if failed * //from w w w . ja v a 2 s . co m * @param url * @param map * Values to request * @return */ static public String doHttpPost(String url, HashMap<String, String> map) { HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT); HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT); ConnManagerParams.setTimeout(client.getParams(), TIMEOUT); HttpPost post = new HttpPost(url); post.setHeaders(headers); String result = "ERROR"; ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>(); if (map != null) { for (Map.Entry<String, String> entry : map.entrySet()) { BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue()); pairList.add(pair); } } try { HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8"); post.setEntity(entity); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { result = EntityUtils.toString(response.getEntity(), "UTF-8"); } else { result = EntityUtils.toString(response.getEntity(), "UTF-8") + response.getStatusLine().getStatusCode() + "ERROR"; } } catch (ConnectTimeoutException e) { result = "TIMEOUTERROR"; } catch (Exception e) { result = "OTHERERROR"; e.printStackTrace(); } return result; }
From source file:org.apache.heron.integration_topology_test.core.HttpUtils.java
public static int httpJsonPost(String newHttpPostUrl, String jsonData) throws IOException, ParseException { HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(newHttpPostUrl); StringEntity requestEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON); post.setEntity(requestEntity);//w ww . j a va 2 s . c o m HttpResponse response = client.execute(post); return response.getStatusLine().getStatusCode(); }
From source file:org.guohai.android.cta.utility.HttpRest.java
/** * HTTPPost?//from w w w .j a v a2 s .c om * @param url POST? * @param pairs ? * @return ?JOSN?null */ public static ResultInfo HttpPostClient(String url, List<NameValuePair> pairs) { //create http client HttpClient client = new DefaultHttpClient(); //create post request HttpPost httpPost = new HttpPost(url); //return value ResultInfo result = new ResultInfo(); //init eroor value result.State = -1000; result.Message = "posterror"; HttpEntity entity; try { entity = new UrlEncodedFormEntity(pairs, HTTP.UTF_8); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e(TAG, e.getMessage()); return result; } httpPost.setEntity(entity); //??POST? try { HttpResponse response = client.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entityHtml = response.getEntity(); BufferedReader reader = new BufferedReader(new InputStreamReader(entityHtml.getContent(), "UTF-8")); String line = null; String reString = ""; while ((line = reader.readLine()) != null) { reString += line; } if (entityHtml != null) { entityHtml.consumeContent(); } Log.i(TAG, reString); JSONObject jsonObj; try { jsonObj = new JSONObject(reString); result.State = jsonObj.getInt("state"); result.Message = jsonObj.getString("message"); return result; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e(TAG, e.getMessage()); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e(TAG, e.getMessage()); } return result; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e(TAG, e.getMessage()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e(TAG, e.getMessage()); } return result; }
From source file:cn.comgroup.tzmedia.server.util.mail.SendCloudMail.java
/** * Send email using SMTP server.//from w w w . ja va 2 s .com * * @param recipientEmail TO recipient * @param title title of the message * @param message message to be sent * connected state or if the message is not a MimeMessage */ public static void send(String recipientEmail, String title, String message) throws UnsupportedEncodingException, IOException { String url = "http://sendcloud.sohu.com/webapi/mail.send.json"; HttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost(url); List nvps = new ArrayList(); // ??SendCloud????????????????? nvps.add(new BasicNameValuePair("api_user", "commobile_test_IxiZE1")); nvps.add(new BasicNameValuePair("api_key", "0tkHZ5vDdScYzRbn")); nvps.add(new BasicNameValuePair("from", "suport@tzzjmedia.net")); nvps.add(new BasicNameValuePair("to", recipientEmail)); nvps.add(new BasicNameValuePair("subject", title)); nvps.add(new BasicNameValuePair("html", message)); httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); // HttpResponse response = httpclient.execute(httpost); // ?? if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // // ?xml String result = EntityUtils.toString(response.getEntity()); Logger.getLogger(SendCloudMail.class.getName()).log(Level.INFO, result); } else { System.err.println("error"); } }
From source file:com.roncoo.pay.permission.utils.RoncooHttpClientUtils.java
/** * API/*from ww w . j a v a 2 s .c om*/ * * @param parameters * @return */ @SuppressWarnings({ "resource", "deprecation" }) public static String post(String url, String parameters) { HttpClient httpClient = new DefaultHttpClient(); HttpPost method = new HttpPost(url); String body = null; if (method != null & parameters != null && !"".equals(parameters.trim())) { try { // NameValuePair?? method.addHeader("Content-type", "application/json; charset=utf-8"); method.setHeader("Accept", "application/json"); method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8"))); HttpResponse response = httpClient.execute(method); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return "1";// 1 } // Read the response body body = EntityUtils.toString(response.getEntity()); } catch (IOException e) { // return "2"; } finally { } } return body; }
From source file:com.world.watch.worldwatchcron.util.PushToUser.java
public static void push(List<String> data, String userId) { try {/*from w ww . j a v a2 s.c o m*/ InputStream jsonStream = PushToUser.class.getResourceAsStream("/parsePush.json"); ObjectMapper mapper = new ObjectMapper(); PushData push = mapper.readValue(jsonStream, PushData.class); push.getWhere().setUsername(userId); push.getData().setKeywords(data); String json = mapper.writeValueAsString(push); HttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().build()) .build(); HttpPost post = new HttpPost(URL); post.setHeader("X-Parse-Application-Id", "WhqWj009luOxOtIH3rM9iWJICLdf0NKbgqdaui8Q"); post.setHeader("X-Parse-REST-API-Key", "lThhKObAz1Tkt092Cl1HeZv4KLUsdATvscOaGN2y"); post.setHeader("Content-Type", "application/json"); logger.debug("JSON to push {}", json.toString()); StringEntity strEntity = new StringEntity(json); post.setEntity(strEntity); httpClient.execute(post); logger.debug("Pushed {} to userId {}", data.toString(), userId); } catch (Exception ex) { logger.error("Push Failed for {} ", userId, ex); } }
From source file:Main.java
/** * Performs an HTTP Post request to the specified url with the * specified parameters, and return the string from the HttpResponse. * * @param url The web address to post the request to * @param postParameters The parameters to send via the request * @return The result string of the request * @throws Exception//from w w w .j a va 2 s . c o m */ public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception { BufferedReader in = null; try { HttpClient client = getHttpClient(); HttpPost request = new HttpPost(url); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String result = sb.toString(); return result; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:apidemo.APIDemo.java
public static String sendPost(String url, byte[] data) throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException { // logger.info("url: " + url); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new ByteArrayEntity(data)); try (CloseableHttpResponse res = httpclient.execute(httpPost)) { HttpEntity entity = res.getEntity(); InputStream inputStream = entity.getContent(); String sResponse = IOUtils.toString(inputStream, "UTF-8"); return sResponse; }/*from w w w. ja v a2 s .co m*/ }