mitao
2025-04-03 71fca447b76d88b45ef5c24b47a9428a517c4499
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.dsh.guns.modular.system.util;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
 
public class HttpRequestUtil {
 
    /***
     * get请求(带参数)
     * @param url
     * @return String
     */
    public static String getReq(String url, Map<String, String> params) {
        String result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            Iterator maplist = params.entrySet().iterator();
            while (maplist.hasNext()) {
                Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next();
                uriBuilder.addParameter(map.getKey(), map.getValue());
            }
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPost get = new HttpPost(uriBuilder.build());
            get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
            HttpResponse response = client.execute(get);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
 
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
           }
        return result;
    }/***
     * get请求(带参数)
     * @param url
     * @return String
     */
    public static String getReq1(String url, Map<String, String> params) {
        String result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            Iterator maplist = params.entrySet().iterator();
            while (maplist.hasNext()) {
                Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next();
                uriBuilder.addParameter(map.getKey(), map.getValue());
            }
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPost get = new HttpPost(uriBuilder.build());
            // todo 验签
            get.setHeader("Authorization", "close");
            get.setHeader("Content-Type", "application/json");
            get.setHeader("Accept", "application/json");
            get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
            HttpResponse response = client.execute(get);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
 
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
           }
        return result;
    }
    public static String postRequest(String url, Map<String, String> params) {
        // 构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
        // 创建POST方法的实例
        PostMethod postMethod = new PostMethod(url);
        // 设置请求头信息
        postMethod.setRequestHeader("Connection", "close");
        postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        // 添加参数
        for (Map.Entry<String, String> entry : params.entrySet()) {
            postMethod.addParameter(entry.getKey(), entry.getValue());
        }
        // 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
        httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        // 接收处理结果
        String result = null;
        try {
            // 执行Http Post请求
            httpClient.executeMethod(postMethod);
            // 返回处理结果
            result = postMethod.getResponseBodyAsString();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            System.out.println("发生网络异常!");
            e.printStackTrace();
        } finally {
            // 释放链接
            postMethod.releaseConnection();
            // 关闭HttpClient实例
            if (httpClient != null) {
                ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
                httpClient = null;
            }
        }
        return result;
    }
    public static String postRequest1(String url, Map<String, Object> params) {
        // 构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
        // 创建POST方法的实例
        PostMethod postMethod = new PostMethod(url);
        // 设置请求头信息
        postMethod.setRequestHeader("Authorization", "close");
        postMethod.setRequestHeader("Accept", "application/json");
        postMethod.addRequestHeader("Content-Type", "application/json");
        postMethod.addRequestHeader("Wechatpay-Serial", "application/json");
        postMethod.setRequestHeader("Connection", "close");
        // 添加参数
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            postMethod.addParameter(entry.getKey(), entry.getValue().toString());
        }
        // 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
        httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        // 接收处理结果
        String result = null;
        try {
            // 执行Http Post请求
            httpClient.executeMethod(postMethod);
            // 返回处理结果
            result = postMethod.getResponseBodyAsString();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            System.out.println("发生网络异常!");
            e.printStackTrace();
        } finally {
            // 释放链接
            postMethod.releaseConnection();
            // 关闭HttpClient实例
            if (httpClient != null) {
                ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
                httpClient = null;
            }
        }
        return result;
    }
    public static String getRequest(String url, Map<String, String> params) {
        // 构造HttpClient实例
        HttpClient client = new HttpClient();
        // 拼接参数
        String paramStr = "";
        for (String key : params.keySet()) {
            paramStr = paramStr + "&" + key + "=" + params.get(key);
        }
        paramStr = paramStr.substring(1);
        // 创建GET方法的实例
        GetMethod method = new GetMethod(url + "?" + paramStr);
        // 接收返回结果
        String result = null;
        try {
            // 执行HTTP GET方法请求
            client.executeMethod(method);
            // 返回处理结果
            result = method.getResponseBodyAsString();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            System.out.println("发生网络异常!");
            e.printStackTrace();
        } finally {
            // 释放链接
            method.releaseConnection();
            // 关闭HttpClient实例
            if (client != null) {
                ((SimpleHttpConnectionManager) client
                        .getHttpConnectionManager()).shutdown();
                client = null;
            }
        }
        return result;
    }
    
    
    /**
     * 发送网络请求
     * @param url
     * @return
     */
    public static String sendNetRequest(String url, Map<String, String> params) {
        // 构造HttpClient实例
        HttpClient client = new HttpClient();
        String paramStr = "";
        for (String key : params.keySet()) {
            paramStr = paramStr + "&" + key + "=" + params.get(key);
        }
        paramStr = paramStr.substring(1);
        System.err.println(url + "?" + paramStr);
        // 创建GET方法的实例
        GetMethod method = new GetMethod(url + "?" + paramStr);
        // 接收返回结果
        String result = null;
        try {
            // 执行HTTP GET方法请求
            client.executeMethod(method);
            // 返回处理结果
            result = method.getResponseBodyAsString();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            System.out.println("发生网络异常!");
            e.printStackTrace();
        } finally {
            // 释放链接
            method.releaseConnection();
            // 关闭HttpClient实例
            if (client != null) {
                ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
                client = null;
            }
        }
        return result;
    }
    
    
    
    /**
     * jsonp跨域请求数据响应<br/>
     * 方法名:responsejsonpData<br/>
     * @author:Mryang<br/>
     * @createTime:2016年7月31日-下午11:17:31 <br/>
     * @tel: 15198268054<br/>
     * @param request
     * @param response
     * @param map void<br/>
     * @exception <br/>
     * @since  1.0.0
     */
    public void responsejsonpData(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
         response.setCharacterEncoding("UTF-8");
         response.setHeader("Content-Type", "text/html;Charset=utf-8");
        try {
            PrintWriter writer = response.getWriter();
            String params = request.getParameter("callback");
            String json = JSONObject.toJSONString(map);
            writer.print(params+ "("+json+")");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}