public JSONObject sendMessage(String url,JSONObject params){ CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(url); post.setHeader("Content-Type","application/json;charset=utf-8"); JSONObject response=new JSONObject(); try{ StringEntity s=new StringEntity(params.toString(),"UTF-8"); s.setChunked(true); s.setContentType("application/json"); post.setEntity(s); HttpResponse res = httpClient.execute(post); if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity entity = res.getEntity(); String result = EntityUtils.toString(res.getEntity());// 返回json格式: response = JSONObject.parseObject(result); } }catch (Exception e){ logger.info("SendMessageToUserImpl.sendMessage Exception:"+e); } logger.info("SendMessageToUserImpl.sendMessage response:"+response.toJSONString()); return response; }
httpClient发送post请求
最新推荐文章于 2024-08-06 17:13:24 发布