chenjiahe
2021-10-15 9fb8897cb017a185d946cae406b81217085d8bbc
提交 | 用户 | 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         JSONObject obj = null;
30         try {
31             StringBuilder dataP = new StringBuilder();
32             if (keyValues != null && !keyValues.isEmpty()) {
33                 for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
34                     dataP.append((String)entry.getKey()).append("=");
35                     dataP.append(entry.getValue());
36                     dataP.append("&");
37                 }
38                 System.out.println("dataP:"+dataP.toString());
39                 dataP.deleteCharAt(dataP.length() - 1);
40                 url = url+"?"+dataP;
41             }
42
43             if(StringUtils.isEmpty(requestMethod)){
44                 requestMethod = "POST";
45             }
46             u = new URL(url);
47             con = (HttpURLConnection) u.openConnection();
48             con.setRequestMethod(requestMethod);
49             con.setDoOutput(true);
50             con.setDoInput(true);
51             con.setUseCaches(false);
52             con.setReadTimeout(5000);
53             con.setRequestProperty("Charset", "UTF-8");
54             con.setRequestProperty("Content-Type", "application/json");
55             if(header != null){
56                 for (Map.Entry<String, String> entry : header.entrySet()) {
57                     con.setRequestProperty(entry.getKey(),entry.getValue());
58                 }
59             }
60
61             if (data != null) {
62                 OutputStream os = con.getOutputStream();
63                 os.write(data.getBytes("utf-8"));
64             }
65
66             if (con.getResponseCode() != 200)
67                 throw new RuntimeException("请求url失败");
68             // 读取返回内容
69             wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
70             // //System.out.println("HttpURLUtil:"+wxMsgXml);
71         } catch (Exception e) {
72             e.printStackTrace();
73             obj = new JSONObject();
74             try {
75                 obj.put("status", 1);
76                 obj.put("errMsg", e.getMessage());
77             } catch (JSONException e1) {
78                 e1.printStackTrace();
79             }
80         } finally {
81             if (con != null) {
82                 con.disconnect();
83             }
84         }
85         return wxMsgXml;
86     }
87
88 }