| | |
| | | package com.dy.common.util; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.KeyManagementException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.cert.X509Certificate; |
| | |
| | | /** |
| | | * get |
| | | * |
| | | * @param host |
| | | * @param path |
| | | * @param headers |
| | | * @param querys |
| | | * @return |
| | | * @throws Exception |
| | | * @param host 服务端URI |
| | | * @param path 请求路径 |
| | | * @param headers 请求头 |
| | | * @param params 请求参数 |
| | | * @return HttpResponse响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static HttpResponse doGet(String host, String path, Map<String, String> headers, |
| | | Map<String, String> querys) throws Exception { |
| | | Map<String, String> params) throws Exception{ |
| | | HttpClient httpClient = wrapClient(host); |
| | | |
| | | HttpGet request = new HttpGet(buildUrl(host, path, querys)); |
| | | HttpGet request = new HttpGet(buildUrl(host, path, params)); |
| | | if(headers != null){ |
| | | for (Map.Entry<String, String> e : headers.entrySet()) { |
| | | request.addHeader(e.getKey(), e.getValue()); |
| | |
| | | |
| | | /** |
| | | * post form |
| | | * |
| | | * @param host |
| | | * @param path |
| | | * @param headers |
| | | * @param querys |
| | | * @param bodys |
| | | * @return |
| | | * @throws Exception |
| | | * |
| | | * @param host 服务端URI |
| | | * @param path 请求路径 |
| | | * @param headers 请求头 |
| | | * @param params 请求参数 |
| | | * @param bodies 请求体 |
| | | * @return HttpResponse响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static HttpResponse doPost(String host, String path, Map<String, String> headers, |
| | | Map<String, String> querys, Map<String, String> bodys) throws Exception { |
| | | Map<String, String> params, Map<String, String> bodies) throws Exception { |
| | | HttpClient httpClient = wrapClient(host); |
| | | |
| | | HttpPost request = new HttpPost(buildUrl(host, path, querys)); |
| | | HttpPost request = new HttpPost(buildUrl(host, path, params)); |
| | | for (Map.Entry<String, String> e : headers.entrySet()) { |
| | | request.addHeader(e.getKey(), e.getValue()); |
| | | } |
| | | |
| | | if (bodys != null) { |
| | | List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); |
| | | if (bodies != null) { |
| | | List<NameValuePair> nameValuePairList = new ArrayList<>(); |
| | | |
| | | for (String key : bodys.keySet()) { |
| | | nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); |
| | | for (String key : bodies.keySet()) { |
| | | nameValuePairList.add(new BasicNameValuePair(key, bodies.get(key))); |
| | | } |
| | | UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); |
| | | formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); |
| | |
| | | |
| | | /** |
| | | * Post String |
| | | * |
| | | * @param host |
| | | * @param path |
| | | * @param headers |
| | | * @param querys |
| | | * @param body |
| | | * @return |
| | | * @throws Exception |
| | | * |
| | | * @param host 服务端URI |
| | | * @param path 请求路径 |
| | | * @param headers 请求头 |
| | | * @param params 请求参数 |
| | | * @param body 请求体 |
| | | * @return HttpResponse响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static HttpResponse doPost(String host, String path, Map<String, String> headers, |
| | | Map<String, String> querys, String body) throws Exception { |
| | | Map<String, String> params, String body) throws Exception { |
| | | HttpClient httpClient = wrapClient(host); |
| | | |
| | | HttpPost request = new HttpPost(buildUrl(host, path, querys)); |
| | | HttpPost request = new HttpPost(buildUrl(host, path, params)); |
| | | for (Map.Entry<String, String> e : headers.entrySet()) { |
| | | request.addHeader(e.getKey(), e.getValue()); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Post stream |
| | | * |
| | | * @param host |
| | | * @param path |
| | | * @param headers |
| | | * @param querys |
| | | * @param body |
| | | * @return |
| | | * @throws Exception |
| | | * |
| | | * @param host 服务端URI |
| | | * @param path 请求路径 |
| | | * @param headers 请求头 |
| | | * @param params 请求参数 |
| | | * @param body 请求体 |
| | | * @return HttpResponse响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static HttpResponse doPost(String host, String path, Map<String, String> headers, |
| | | Map<String, String> querys, byte[] body) throws Exception { |
| | | Map<String, String> params, byte[] body) throws Exception { |
| | | HttpClient httpClient = wrapClient(host); |
| | | |
| | | HttpPost request = new HttpPost(buildUrl(host, path, querys)); |
| | | HttpPost request = new HttpPost(buildUrl(host, path, params)); |
| | | if(headers != null){ |
| | | for (Map.Entry<String, String> e : headers.entrySet()) { |
| | | request.addHeader(e.getKey(), e.getValue()); |
| | |
| | | |
| | | /** |
| | | * Put String |
| | | * |
| | | * @param host |
| | | * @param path |
| | | * @param headers |
| | | * @param querys |
| | | * @param body |
| | | * @return |
| | | * @throws Exception |
| | | * |
| | | * @param host 服务端URI |
| | | * @param path 请求路径 |
| | | * @param headers 请求头 |
| | | * @param params 请求参数 |
| | | * @param body 请求体 |
| | | * @return HttpResponse响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static HttpResponse doPut(String host, String path, Map<String, String> headers, |
| | | Map<String, String> querys, String body) throws Exception { |
| | | Map<String, String> params, String body) throws Exception { |
| | | HttpClient httpClient = wrapClient(host); |
| | | |
| | | HttpPut request = new HttpPut(buildUrl(host, path, querys)); |
| | | HttpPut request = new HttpPut(buildUrl(host, path, params)); |
| | | for (Map.Entry<String, String> e : headers.entrySet()) { |
| | | request.addHeader(e.getKey(), e.getValue()); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Put stream |
| | | * |
| | | * @param host |
| | | * @param path |
| | | * @param headers |
| | | * @param querys |
| | | * @param body |
| | | * @return |
| | | * @throws Exception |
| | | * |
| | | * @param host 服务端URI |
| | | * @param path 请求路径 |
| | | * @param headers 请求头 |
| | | * @param params 请求参数 |
| | | * @param body 请求体 |
| | | * @return HttpResponse响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static HttpResponse doPut(String host, String path, Map<String, String> headers, |
| | | Map<String, String> querys, byte[] body) throws Exception { |
| | | Map<String, String> params, byte[] body) throws Exception { |
| | | HttpClient httpClient = wrapClient(host); |
| | | |
| | | HttpPut request = new HttpPut(buildUrl(host, path, querys)); |
| | | HttpPut request = new HttpPut(buildUrl(host, path, params)); |
| | | for (Map.Entry<String, String> e : headers.entrySet()) { |
| | | request.addHeader(e.getKey(), e.getValue()); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Delete |
| | | * |
| | | * @param host |
| | | * @param path |
| | | * @param headers |
| | | * @param querys |
| | | * @return |
| | | * @throws Exception |
| | | * |
| | | * @param host 服务端URI |
| | | * @param path 请求路径 |
| | | * @param headers 请求头 |
| | | * @param params 请求参数 |
| | | * @return HttpResponse响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static HttpResponse doDelete(String host, String path, Map<String, String> headers, |
| | | Map<String, String> querys) throws Exception { |
| | | Map<String, String> params) throws Exception { |
| | | HttpClient httpClient = wrapClient(host); |
| | | |
| | | HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); |
| | | HttpDelete request = new HttpDelete(buildUrl(host, path, params)); |
| | | for (Map.Entry<String, String> e : headers.entrySet()) { |
| | | request.addHeader(e.getKey(), e.getValue()); |
| | | } |
| | |
| | | return httpClient.execute(request); |
| | | } |
| | | |
| | | private static String buildUrl(String host, String path, Map<String, String> querys) |
| | | throws UnsupportedEncodingException { |
| | | private static String buildUrl(String host, String path, Map<String, String> params) { |
| | | StringBuilder sbUrl = new StringBuilder(); |
| | | sbUrl.append(host); |
| | | if (!StringUtils.isBlank(path)) { |
| | | sbUrl.append(path); |
| | | } |
| | | if (null != querys) { |
| | | if (null != params) { |
| | | StringBuilder sbQuery = new StringBuilder(); |
| | | for (Map.Entry<String, String> query : querys.entrySet()) { |
| | | for (Map.Entry<String, String> query : params.entrySet()) { |
| | | if (0 < sbQuery.length()) { |
| | | sbQuery.append("&"); |
| | | } |
| | |
| | | sbQuery.append(query.getKey()); |
| | | if (!StringUtils.isBlank(query.getValue())) { |
| | | sbQuery.append("="); |
| | | sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); |
| | | sbQuery.append(URLEncoder.encode(query.getValue(), StandardCharsets.UTF_8)); |
| | | } |
| | | } |
| | | } |