java利用httpclient傳送post請求 ,請求資料放到body裡。
/** * post請求 ,請求資料放到body裡 * * @author lifq * * 2017年3月15日 下午3:47:04 */ public static String doPostBodyData(String url, String bodyData) throws Exception { String result = ""; CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try { HttpPost httpPost = getHttpPost(url, null); // 請求地址 httpPost.setEntity(new StringEntity(bodyData, Encoding)); httpClient = getHttpClient(); // 得到返回的response. response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); result = getResult(entity, Encoding); } catch (Exception e) { throw e; } finally { // 關閉httpClient if (null != httpClient) { httpClient.close(); } // 關閉response if (null != response) { EntityUtils.consume(response.getEntity()); // 會自動釋放連線 response.close(); } } return result; }
限時領取免費Java相關資料,涵蓋了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高併發分散式、大資料、機器學習等技術。
資料傳送門:https://mp.weixin.qq.com/s/u2b_NVNuMuAPE0w4lc45fw
關注下方公眾號即可免費領取: