HttpsRequestUtil.java
4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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<String,String> 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<String,Object> headerMap,String sRequestType,String contentType) {
return this.doRequestHttp( url, param, headerMap, sRequestType, contentType);
}
/** 调用 url ,调用 JSON,头部传参数 */
public String doRequestHttp(String apiUrl, String sBody,
Map<String,Object> 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<String,Object> 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<String,Object> 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<String,Object> 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();
}
}