执行post提交 @param url 提交的URL @param paramMap 参数集合 @return @throws IOException
(String url, Map<String, String> paramMap)
| 79 | * @throws IOException |
| 80 | */ |
| 81 | public HttpResult doPost(String url, Map<String, String> paramMap) throws IOException { |
| 82 | HttpPost httpPost = new HttpPost(url); |
| 83 | |
| 84 | |
| 85 | // httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); |
| 86 | |
| 87 | //设置请求参数 |
| 88 | httpPost.setConfig(requestConfig); |
| 89 | if (paramMap != null) { |
| 90 | List<NameValuePair> parameters = new ArrayList<NameValuePair>(); |
| 91 | for (String s : paramMap.keySet()) { |
| 92 | parameters.add(new BasicNameValuePair(s, paramMap.get(s))); |
| 93 | } |
| 94 | //构建一个form表单式的实体 |
| 95 | UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, StandardCharsets.UTF_8); |
| 96 | //将请求实体放入到httpPost中 |
| 97 | httpPost.setEntity(formEntity); |
| 98 | } |
| 99 | //创建httpClient对象 |
| 100 | CloseableHttpResponse response = null; |
| 101 | try { |
| 102 | //执行请求 |
| 103 | response = httpClient.execute(httpPost); |
| 104 | return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(),"UTF-8")); |
| 105 | } catch (IOException e) { |
| 106 | throw e; |
| 107 | } finally { |
| 108 | if (response != null) { |
| 109 | response.close(); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * 执行post提交 |