package com.xly.util; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.Header; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONUtil; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.util.HashMap; import java.util.Map; /**\ * HTTP 调用接口 * @author qianbao */ public class HttpsRequestUtil { private static final HttpsRequestUtil me = new HttpsRequestUtil(); private static final String GET ="GET"; private static final String MAP ="MAP"; private static final String charset_def ="UTF-8"; private static final String checkUrlString ="https"; public static HttpsRequestUtil me() { return me; } private static Map contentTypeMap = new HashMap<>(4); static { contentTypeMap.put("JSON","application/json"); contentTypeMap.put("XML","text/xml"); contentTypeMap.put("MAP","application/x-www-form-urlencoded; charset=UTF-8"); } /** 调用 url ,调用 JSON,头部传参数 */ public String doRequestHttps(String url, String param, Map headerMap,String sRequestType,String contentType) { return this.doRequestHttp( url, param, headerMap, sRequestType, contentType); } /** 调用 url ,调用 JSON,头部传参数 */ public String doRequestHttp(String apiUrl, String sBody, Map headerMap, String sMethod, String sBodyType ) { HttpRequest hr; if (GET.equalsIgnoreCase(sMethod)){ hr = HttpUtil.createGet(apiUrl); }else { hr = HttpUtil.createPost(apiUrl); } if(StrUtil.isNotEmpty(sBody)){ Boolean bParam = "MAP".equals(sBodyType); if(bParam){ if(JSONUtil.isJsonObj(sBody)){ Map map2 = JSONUtil.parseObj(sBody); hr.form(map2); } }else if(JSONUtil.isJson(sBody)){ hr.body(JSONUtil.toJsonPrettyStr(sBody)); }else{ hr.body(sBody); } } // hr.header(Header.CONTENT_TYPE,contentTypeMap.get(contentType)).charset(charset); if(apiUrl.toLowerCase().startsWith(checkUrlString)){ hr.header("X-Bmob-Application-Id","2f0419a31f9casdfdsf431f6cd297fdd3e28fds4af") .header("X-Bmob-REST-API-Key","1e03efdas82178723afdsafsda4be0f305def6708cc6"); } //设置请求超时时间20S hr.setConnectionTimeout(6000000); hr.setReadTimeout(6000000); if(MapUtil.isNotEmpty(headerMap)){ headerMap.forEach((k,v)->{ String value =""; if(ObjectUtil.isNotEmpty(v)){ value = v.toString(); } hr.header(k,value); }); } return hr.execute().body(); } public String doRequestHttps(String url, String param, Map headerMap,String sRequestType,String contentType,String charset) { HttpRequest hr; if (GET.equalsIgnoreCase(sRequestType)){ hr = HttpUtil.createGet(url); }else { hr = HttpUtil.createPost(url); } if(StrUtil.isNotEmpty(param)){ Boolean bParam = "MAP".equals(contentType); if(bParam){ if(JSONUtil.isJsonObj(param)){ Map map2 = JSONUtil.parseObj(param); hr.form(map2); } }else if(JSONUtil.isJson(param)){ hr.body(JSONUtil.toJsonPrettyStr(param)); }else{ hr.body(param); } } hr.header(Header.CONTENT_TYPE,contentTypeMap.get(contentType)).charset(charset) .header("X-Bmob-Application-Id","2f0419a31f9casdfdsf431f6cd297fdd3e28fds4af") .header("X-Bmob-REST-API-Key","1e03efdas82178723afdsafsda4be0f305def6708cc6"); //设置请求超时时间20S hr.setConnectionTimeout(600000); if(MapUtil.isNotEmpty(headerMap)){ headerMap.forEach((k,v)->{ String value =""; if(ObjectUtil.isNotEmpty(v)){ value = v.toString(); } hr.header(k,value); }); } return hr.execute().body(); } }