發送httpPost請求
阿新 • • 發佈:2017-10-27
param response ava true lose message post 解決 信息
public static JSONObject sendPost(JSONObject jsonParam, String url) { LOGGER.info("獲取回執信息請求參數:"+jsonParam.toString()); JSONObject resultJson = null; //創建httpclient對象 CloseableHttpClient client = HttpClients.createDefault(); //創建post方式請求對象 HttpPost httpPost = new HttpPost(url); StringEntity entity=null; if(jsonParam != null) { entity = new StringEntity(jsonParam.toString(),"utf-8");// 解決中文亂碼問題 } entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); // 發起請求 HttpResponse httpResponse = null; String resData = null; try { httpResponse = client.execute(httpPost); resData = EntityUtils.toString(httpResponse.getEntity(), Charsets.UTF_8.name()); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } finally { try { client.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } resultJson = JSON.parseObject(resData); return resultJson; }
發送httpPost請求