复制代码
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205import com.alibaba.fastjson.JSON; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Map; public class HttpClientUtil { private static final CloseableHttpClient httpClient = HttpClients.createDefault(); private static final RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(2000) .setSocketTimeout(10000).build(); public static String doGet(String url) { return doGet(url, null); } public static String doPost(String url) { return doPost(url, null); } public static String doGet(String url, Map<String, String> param) { //创建HttpClient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { //创建URI URIBuilder builder = new URIBuilder(url); //设置参数 if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); //创建Http请求 HttpGet httpGet = new HttpGet(uri); //执行请求 response = httpClient.execute(httpGet); //如果成功,返回响应字符串 if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } public static String doPost(String url, Map<String, String> param) { //创建HttpClient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { //创建HttpPost请求方式 HttpPost httpPost = new HttpPost(url); //创建参数列表 if (param != null) { List<NameValuePair> paramList = new ArrayList<>(); for (String key : param.keySet()) { paramList.add(new BasicNameValuePair(key, param.get(key))); } //模拟表单 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); httpPost.setEntity(entity); } //执行http请求 response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } public static String doPostJosn(String url, String json) { //创建HttpClient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { //创建post请求 HttpPost httpPost = new HttpPost(); //创建请求内容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); //执行请求 response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } return resultString; } public static JSONObject getJson(String res){ JSONObject jsonObj = new JSONObject(res); return jsonObj; } /** * POST请求 携带Json格式的参数 * * @param url * @param param * @return * @throws IOException */ public static String postJson(String url, Object param) { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); httpPost.setConfig(requestConfig); String parameter = JSON.toJSONString(param); StringEntity se = null; try { se = new StringEntity(parameter); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } se.setContentType("text/json"); httpPost.setEntity(se); CloseableHttpResponse response = null; String result = null; try { response = httpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); result = EntityUtils.toString(httpEntity, "UTF-8"); } catch (IOException e) { e.printStackTrace(); } return result; } }
使用案例
复制代码
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//1.get请求-不带参 String url = loginOut+"/"+ticket; String res = HttpClientUtil.doGet(url); JSONObject json = HttpClientUtil.getJson(res); //2.get请求-带参 Map<String,String> param = new HashMap<>(); param.put("token",token); String res = HttpClientUtil.doGet(getUserInfo,param); JSONObject jsonRes = HttpClientUtil.getJson(res); //3.post请求(非json格式) Map<String,String> param = new HashMap<>(); param.put("username",phone); param.put("password",passwordOld); String res = HttpClientUtil.doPost(loginTest, param); JSONObject json = HttpClientUtil.getJson(res); //4.post请求(json格式的参数) com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject(); object.put("mobile",mobile); object.put("type",18); object.put("param",new Object()); object.put("signName",""); String res = HttpClientUtil.postJson(messageSend, object); log.info("发送短信res:"+res); JSONObject json = HttpClientUtil.getJson(res);
最后
以上就是柔弱画板最近收集整理的关于自定义Http请求(get、post)的全部内容,更多相关自定义Http请求(get、post)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复