| | |
| | | return postJson(url, json, header); |
| | | } |
| | | |
| | | public String doPostJsons(String url, Map<String, Object> params, Map<String, String> header) throws Exception { |
| | | String json = null; |
| | | if (params != null && !params.isEmpty()) { |
| | | for (Iterator<Entry<String, Object>> it = params.entrySet().iterator(); it.hasNext();) { |
| | | Entry<String, Object> entry = (Entry<String, Object>) it.next(); |
| | | Object object = entry.getValue(); |
| | | if (object == null) { |
| | | it.remove(); |
| | | } |
| | | } |
| | | json = JSON.toJSONString(params); |
| | | } |
| | | return postJsons(url, json, header); |
| | | } |
| | | |
| | | public String doPostJson(String url, String json) throws Exception { |
| | | return doPostJson(url, json, null); |
| | | } |
| | |
| | | return body; |
| | | } |
| | | |
| | | private String postJsons(String url, String json, Map<String, String> header) throws Exception { |
| | | String body = null; |
| | | try { |
| | | // Post请求 |
| | | LOG.debug(" protocol: POST"); |
| | | LOG.debug(" url: " + url); |
| | | HttpPost httpPost = new HttpPost(url.trim()); |
| | | // 设置参数 |
| | | LOG.debug(" params: " + json); |
| | | httpPost.setEntity(new StringEntity(json, ContentType.DEFAULT_TEXT.withCharset(charset))); |
| | | httpPost.setHeader(new BasicHeader("Content-Type", "application/json")); |
| | | LOG.debug(" type: JSON"); |
| | | // 设置Header |
| | | if (header != null && !header.isEmpty()) { |
| | | LOG.debug(" header: " + JSON.toJSONString(header)); |
| | | for (Iterator<Entry<String, String>> it = header.entrySet().iterator(); it.hasNext();) { |
| | | Entry<String, String> entry = (Entry<String, String>) it.next(); |
| | | httpPost.setHeader(new BasicHeader(entry.getKey(), entry.getValue())); |
| | | } |
| | | } |
| | | // 发送请求,获取返回数据 |
| | | body = execute(httpPost); |
| | | } catch (Exception e) { |
| | | throw e; |
| | | } |
| | | LOG.debug(" result: " + body); |
| | | return body; |
| | | } |
| | | |
| | | /** |
| | | * get请求 |
| | | */ |