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