chenjiahe
2021-10-20 e10d4111e73a502fcd5f51c47312c754dcf915b8
提交 | 用户 | age
9fb889 1 package com.hx.util;
C 2
3 import net.sf.json.JSONException;
4 import net.sf.json.JSONObject;
5 import org.apache.commons.io.IOUtils;
6
7 import java.io.OutputStream;
8 import java.net.HttpURLConnection;
9 import java.net.URL;
10 import java.util.Map;
11
12 /**
13  * http 工具类
14  */
15 public class HttpMethodUtil {
16
17     /** 请求http协议 获取信息工具
18      * @param url 请求链接
19      * @param data 请求数据(body)
20      * @param keyValues form表单数据 key参数名称,value参数值
21      * @param header 请求头
22      * @param requestMethod 请求头方法,默认POST
23      * @return
24      */
25     public static String HttpURLUtilJson(String url, String data,Map<String,Object> keyValues,Map<String,String> header,String requestMethod) {
26         HttpURLConnection con = null;
27         URL u = null;
28         String wxMsgXml = null;
29         try {
30             StringBuilder dataP = new StringBuilder();
31             if (keyValues != null && !keyValues.isEmpty()) {
32                 for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
33                     dataP.append((String)entry.getKey()).append("=");
34                     dataP.append(entry.getValue());
35                     dataP.append("&");
36                 }
37                 System.out.println("dataP:"+dataP.toString());
38                 dataP.deleteCharAt(dataP.length() - 1);
39                 url = url+"?"+dataP;
40             }
41
42             if(StringUtils.isEmpty(requestMethod)){
43                 requestMethod = "POST";
44             }
45             u = new URL(url);
46             con = (HttpURLConnection) u.openConnection();
47             con.setRequestMethod(requestMethod);
48             con.setDoOutput(true);
49             con.setDoInput(true);
50             con.setUseCaches(false);
51             con.setReadTimeout(5000);
52             con.setRequestProperty("Charset", "UTF-8");
53             con.setRequestProperty("Content-Type", "application/json");
54             if(header != null){
55                 for (Map.Entry<String, String> entry : header.entrySet()) {
56                     con.setRequestProperty(entry.getKey(),entry.getValue());
57                 }
58             }
59
60             if (data != null) {
61                 OutputStream os = con.getOutputStream();
62                 os.write(data.getBytes("utf-8"));
63             }
64
b82acb 65             if (con.getResponseCode() != 200){
9fb889 66                 throw new RuntimeException("请求url失败");
b82acb 67             }
9fb889 68             // 读取返回内容
C 69             wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
70             // //System.out.println("HttpURLUtil:"+wxMsgXml);
71         } catch (Exception e) {
72             e.printStackTrace();
73         } finally {
74             if (con != null) {
75                 con.disconnect();
76             }
77         }
78         return wxMsgXml;
79     }
80
81 }