xiaoyong931011
2021-06-25 3c7591be842397c26d6ea7dbd11a3b5bd1751dd1
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.xzx.gc.util.pay.util;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.*;
import java.util.Iterator;
import java.util.Map;
 
 
public class HttpRequestor {
    public static String charset = "utf-8";
    public static Integer connectTimeout = null;
    public static Integer socketTimeout = null;
    public static  String proxyHost = null;
    public static Integer proxyPort = null;
 
    /**
     * Do GET request
     * @param url
     * @return
     * @throws Exception
     * @throws IOException
     */
    public static String doGet(String url) throws Exception {
 
        URL localURL = new URL(url);
 
        URLConnection connection = openConnection(localURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
 
        httpURLConnection.setRequestProperty("Accept-Charset", charset);
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
 
        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }
 
        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            reader = new BufferedReader(inputStreamReader);
 
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
 
        } finally {
 
            if (reader != null) {
                reader.close();
            }
 
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
 
            if (inputStream != null) {
                inputStream.close();
            }
 
        }
 
        return resultBuffer.toString();
    }
 
    /**
     * Do POST request
     * @param url
     * @param parameterMap
     * @return
     * @throws Exception
     */
    public String doPost(String url, Map parameterMap) throws Exception {
 
        /* Translate parameter map to parameter date string */
        StringBuffer parameterBuffer = new StringBuffer();
        if (parameterMap != null) {
            Iterator iterator = parameterMap.keySet().iterator();
            String key = null;
            String value = null;
            while (iterator.hasNext()) {
                key = (String)iterator.next();
                if (parameterMap.get(key) != null) {
                    value = (String)parameterMap.get(key);
                } else {
                    value = "";
                }
 
                parameterBuffer.append(key).append("=").append(value);
                if (iterator.hasNext()) {
                    parameterBuffer.append("&");
                }
            }
        }
 
        System.out.println("POST parameter : " + parameterBuffer.toString());
 
        URL localURL = new URL(url);
 
        URLConnection connection = openConnection(localURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
 
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept-Charset", charset);
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterBuffer.length()));
 
        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
 
        try {
            outputStream = httpURLConnection.getOutputStream();
            outputStreamWriter = new OutputStreamWriter(outputStream);
 
            outputStreamWriter.write(parameterBuffer.toString());
            outputStreamWriter.flush();
 
            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }
 
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
 
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
 
        } finally {
 
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
 
            if (outputStream != null) {
                outputStream.close();
            }
 
            if (reader != null) {
                reader.close();
            }
 
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
 
            if (inputStream != null) {
                inputStream.close();
            }
 
        }
 
        return resultBuffer.toString();
    }
 
    public static URLConnection openConnection(URL localURL) throws IOException {
        URLConnection connection;
        if (proxyHost != null && proxyPort != null) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            connection = localURL.openConnection(proxy);
        } else {
            connection = localURL.openConnection();
        }
        return connection;
    }
 
    /**
     * Render request according setting
     * @param connection
     */
    private void renderRequest(URLConnection connection) {
 
        if (connectTimeout != null) {
            connection.setConnectTimeout(connectTimeout);
        }
 
        if (socketTimeout != null) {
            connection.setReadTimeout(socketTimeout);
        }
 
    }
 
    /*
     * Getter & Setter
     */
    public Integer getConnectTimeout() {
        return connectTimeout;
    }
 
    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }
 
    public Integer getSocketTimeout() {
        return socketTimeout;
    }
 
    public void setSocketTimeout(Integer socketTimeout) {
        this.socketTimeout = socketTimeout;
    }
 
    public String getProxyHost() {
        return proxyHost;
    }
 
    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }
 
    public Integer getProxyPort() {
        return proxyPort;
    }
 
    public void setProxyPort(Integer proxyPort) {
        this.proxyPort = proxyPort;
    }
 
    public String getCharset() {
        return charset;
    }
 
    public void setCharset(String charset) {
        this.charset = charset;
    }
 
 
    public static void main(String[] args) {
        String code="";
        String appid = "wx6d7dfd9073dfa875";
        String secret = "4d3aea976157935e563f8ef01c7a4293";
        String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
        try {
            String  openid = new HttpRequestor().doGet(requestUrl);
            System.out.println(openid);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
      /*  String code = request.getParameter("code");
        String appid = "wx4b4009c4fce00e0c";
        String secret = "4d3aea976157935e563f8ef01c7a4293";
        String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+wxCode+"&grant_type=authorization_code";
       */ //第一次请求 获取access_token 和 openid
       /* String  oppid = new HttpRequestor().doGet(requestUrl);
        JSONObject oppidObj =JSONObject.fromObject(oppid);
        String access_token = (String) oppidObj.get("access_token");
        String openid = (String) oppidObj.get("openid");
        String requestUrl2 = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
        String userInfoStr = new HttpRequestor().doGet(requestUrl2);
        JSONObject wxUserInfo =JSONObject.fromObject(userInfoStr);*/
    }
}