fwq
2024-09-27 9436a044624dfc8ab027a5345d9814d29a0f74f3
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
package com.hx.util;
 
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
 
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
/**
 * http 工具类
 */
public class HttpMethodUtil {
 
    /** 请求http协议 获取信息工具
     * @param url 请求链接
     * @param data 请求数据(body)
     * @param keyValues form表单数据 key参数名称,value参数值
     * @param header 请求头
     * @param requestMethod 请求头方法,默认POST
     * @return
     */
    public static String HttpURLUtilJson(String url, String data,Map<String,Object> keyValues,Map<String,String> header,String requestMethod) {
        HttpURLConnection con = null;
        URL u = null;
        String wxMsgXml = null;
        try {
            StringBuilder dataP = new StringBuilder();
            if (keyValues != null && !keyValues.isEmpty()) {
                for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
                    dataP.append((String)entry.getKey()).append("=");
                    dataP.append(entry.getValue());
                    dataP.append("&");
                }
                System.out.println("dataP:"+dataP.toString());
                dataP.deleteCharAt(dataP.length() - 1);
                url = url+"?"+dataP;
            }
 
            if(StringUtils.isEmpty(requestMethod)){
                requestMethod = "POST";
            }
            u = new URL(url);
            con = (HttpURLConnection) u.openConnection();
            con.setRequestMethod(requestMethod);
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setReadTimeout(300000);
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type", "application/json");
            if(header != null){
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    con.setRequestProperty(entry.getKey(),entry.getValue());
                }
            }
 
            if (data != null) {
                OutputStream os = con.getOutputStream();
                os.write(data.getBytes("utf-8"));
            }
 
            if(HttpURLConnection.HTTP_OK != con.getResponseCode() && HttpURLConnection.HTTP_CREATED != con.getResponseCode()){
                throw new RuntimeException("请求url失败:"+con.getResponseCode());
            }
            // 读取返回内容
            wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
            // //System.out.println("HttpURLUtil:"+wxMsgXml);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
        return wxMsgXml;
    }
 
    /**
     * 请求http协议 获取信息工具
     * @param url 请求链接
     * @param data 请求数据(body)
     * @param keyValues form表单数据 key参数名称,value参数值
     * @param header 请求头
     * @param requestMethod 请求头方法,默认POST
     * @return
     */
    public static String request(String url, String data,Map<String,Object> keyValues,Map<String,String> header,String requestMethod, String contentType) {
        HttpURLConnection con = null;
        URL u = null;
        String wxMsgXml = null;
        try {
            StringBuilder dataP = new StringBuilder();
            if (keyValues != null && !keyValues.isEmpty()) {
                for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
                    dataP.append((String)entry.getKey()).append("=");
                    dataP.append(entry.getValue());
                    dataP.append("&");
                }
                System.out.println("dataP:"+dataP.toString());
                dataP.deleteCharAt(dataP.length() - 1);
                url = url+"?"+dataP;
            }
 
            if(StringUtils.isEmpty(requestMethod)){
                requestMethod = "POST";
            }
 
            if(StringUtils.isEmpty(contentType)){
                contentType = "application/json";
            }
 
            u = new URL(url);
            con = (HttpURLConnection) u.openConnection();
            con.setRequestMethod(requestMethod);
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setReadTimeout(300000);
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type", contentType);
            if(header != null){
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    con.setRequestProperty(entry.getKey(),entry.getValue());
                }
            }
 
            if (data != null) {
                OutputStream os = con.getOutputStream();
                os.write(data.getBytes("utf-8"));
            }
 
            if(HttpURLConnection.HTTP_OK != con.getResponseCode() && HttpURLConnection.HTTP_CREATED != con.getResponseCode()){
                throw new RuntimeException("请求url失败:"+con.getResponseCode());
            }
            // 读取返回内容
            wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
            // //System.out.println("HttpURLUtil:"+wxMsgXml);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
        return wxMsgXml;
    }
 
}