执行post请求 @param url URL地址 @param paramMap 请求参数 @return @throws IOException
(String url, Map<String, String> paramMap)
| 97 | * @throws IOException |
| 98 | */ |
| 99 | public HttpResult doPost(String url, Map<String, String> paramMap) throws IOException { |
| 100 | HttpPost httpPost = new HttpPost(url); |
| 101 | |
| 102 | |
| 103 | // httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); |
| 104 | |
| 105 | //设置请求参数 |
| 106 | httpPost.setConfig(requestConfig); |
| 107 | if (paramMap != null) { |
| 108 | List<NameValuePair> parameters = new ArrayList<NameValuePair>(); |
| 109 | for (String s : paramMap.keySet()) { |
| 110 | parameters.add(new BasicNameValuePair(s, paramMap.get(s))); |
| 111 | } |
| 112 | //构建一个form表单式的实体 |
| 113 | UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, Charset.forName("UTF-8")); |
| 114 | //将请求实体放入到httpPost中 |
| 115 | httpPost.setEntity(formEntity); |
| 116 | } |
| 117 | //创建httpClient对象 |
| 118 | CloseableHttpResponse response = null; |
| 119 | try { |
| 120 | //执行请求 |
| 121 | response = httpClient.execute(httpPost); |
| 122 | return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); |
| 123 | } finally { |
| 124 | if (response != null) { |
| 125 | response.close(); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * 执行post请求 |
no test coverage detected