cmg
2024-06-22 cb38789006b411b29fba27a6a60493dc78946e17
提交 | 用户 | age
9fb889 1 package com.hx.util;
C 2
e6dfe7 3 import com.alibaba.fastjson.JSONObject;
9fb889 4 import org.apache.commons.io.IOUtils;
C 5
6 import java.io.OutputStream;
7 import java.net.HttpURLConnection;
8 import java.net.URL;
e6dfe7 9 import java.util.HashMap;
9fb889 10 import java.util.Map;
C 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);
ef9f1c 51             con.setReadTimeout(300000);
9fb889 52             con.setRequestProperty("Charset", "UTF-8");
C 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
21f06d 65             if(HttpURLConnection.HTTP_OK != con.getResponseCode() && HttpURLConnection.HTTP_CREATED != con.getResponseCode()){
220f21 66                 throw new RuntimeException("请求url失败:"+con.getResponseCode());
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
50b181 81     /**
F 82      * 请求http协议 获取信息工具
e6dfe7 83      * @param url 请求链接
F 84      * @param data 请求数据(body)
85      * @param keyValues form表单数据 key参数名称,value参数值
86      * @param header 请求头
87      * @param requestMethod 请求头方法,默认POST
88      * @return
89      */
90     public static String request(String url, String data,Map<String,Object> keyValues,Map<String,String> header,String requestMethod, String contentType) {
91         HttpURLConnection con = null;
92         URL u = null;
93         String wxMsgXml = null;
94         try {
95             StringBuilder dataP = new StringBuilder();
96             if (keyValues != null && !keyValues.isEmpty()) {
97                 for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
98                     dataP.append((String)entry.getKey()).append("=");
99                     dataP.append(entry.getValue());
100                     dataP.append("&");
101                 }
102                 System.out.println("dataP:"+dataP.toString());
103                 dataP.deleteCharAt(dataP.length() - 1);
104                 url = url+"?"+dataP;
105             }
106
107             if(StringUtils.isEmpty(requestMethod)){
108                 requestMethod = "POST";
109             }
110
111             if(StringUtils.isEmpty(contentType)){
112                 contentType = "application/json";
113             }
114
115             u = new URL(url);
116             con = (HttpURLConnection) u.openConnection();
117             con.setRequestMethod(requestMethod);
118             con.setDoOutput(true);
119             con.setDoInput(true);
120             con.setUseCaches(false);
121             con.setReadTimeout(300000);
122             con.setRequestProperty("Charset", "UTF-8");
123             con.setRequestProperty("Content-Type", contentType);
124             if(header != null){
125                 for (Map.Entry<String, String> entry : header.entrySet()) {
126                     con.setRequestProperty(entry.getKey(),entry.getValue());
127                 }
128             }
129
130             if (data != null) {
131                 OutputStream os = con.getOutputStream();
132                 os.write(data.getBytes("utf-8"));
133             }
134
135             if(HttpURLConnection.HTTP_OK != con.getResponseCode() && HttpURLConnection.HTTP_CREATED != con.getResponseCode()){
136                 throw new RuntimeException("请求url失败:"+con.getResponseCode());
137             }
138             // 读取返回内容
139             wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
140             // //System.out.println("HttpURLUtil:"+wxMsgXml);
141         } catch (Exception e) {
142             e.printStackTrace();
143         } finally {
144             if (con != null) {
145                 con.disconnect();
146             }
147         }
148         return wxMsgXml;
149     }
150
9fb889 151 }