北京经济技术开发区经开区虚拟城市项目-【后端】-服务,Poi,企业,地块等定制接口
AdaKing88
2023-07-26 1651fb641f0eb793002d00ec51edf1af73376f81
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
271
272
273
274
275
276
277
278
279
package com.smartearth.poiexcel.utils;
 
import javax.net.ssl.*;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.cert.X509Certificate;
import java.util.Map;
 
public class HttpUtils {
 
    private static final String TAG = "HttpUtils";
    private static final int mReadTimeOut = 1000 * 10; // 10秒
    private static final int mConnectTimeOut = 1000 * 5; // 5秒
    //    private static final String CHAR_SET = Constant.ENCODING_UTF_8;
    private static final String CHAR_SET = "UTF-8";
    private static final int mRetry = 2; // 默认尝试访问次数
 
 
 
 
 
    public static String get(String url) throws Exception {
        return get(url, null);
    }
 
    public static String get(String url, Map<String, ? extends Object> params) throws Exception {
        return get(url, params, null);
    }
 
    public static String get(String url, Map<String, ? extends Object> params, Map<String, String> headers)
            throws Exception {
        if (url == null || url.trim().length() == 0) {
            throw new Exception(TAG + ": url is null or empty!");
        }
 
        if (params != null && params.size() > 0) {
            if (!url.contains("?")) {
                url += "?";
            }
 
            if (url.charAt(url.length() - 1) != '?') {
                url += "&";
            }
 
            url += buildParams(params);
        }
 
        return tryToGet(url, headers);
    }
 
    public static String buildParams(Map<String, ? extends Object> params) throws UnsupportedEncodingException {
        if (params == null || params.isEmpty()) {
            return null;
        }
 
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, ? extends Object> entry : params.entrySet()) {
            if (entry.getKey() != null && entry.getValue() != null)
                builder.append(entry.getKey().trim()).append("=")
                        .append(URLEncoder.encode(entry.getValue().toString(), CHAR_SET)).append("&");
        }
 
        if (builder.charAt(builder.length() - 1) == '&') {
            builder.deleteCharAt(builder.length() - 1);
        }
 
        return builder.toString();
    }
 
    private static String tryToGet(String url, Map<String, String> headers) throws Exception {
        int tryTime = 0;
        Exception ex = null;
        while (tryTime < mRetry) {
            try {
                return doGet(url, headers);
            } catch (Exception e) {
                if (e != null)
                    ex = e;
                tryTime++;
            }
        }
        if (ex != null)
            throw ex;
        else
            throw new Exception("未知网络错误 ");
    }
 
    private static String doGet(String strUrl, Map<String, String> headers) throws Exception {
        HttpURLConnection connection = null;
        InputStream stream = null;
        try {
 
            connection = getConnection(strUrl);
            configConnection(connection);
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
 
            connection.setInstanceFollowRedirects(true);
            connection.connect();
 
            stream = connection.getInputStream();
            ByteArrayOutputStream obs = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            for (int len = 0; (len = stream.read(buffer)) > 0;) {
                obs.write(buffer, 0, len);
            }
            obs.flush();
            obs.close();
            stream.close();
 
            return new String(obs.toByteArray());
        } finally {
            if (stream != null) {
                stream.close();
            }
            if (connection != null) {
                connection.disconnect();
                connection=null;
            }
        }
    }
 
    public static String post(String url) throws Exception {
        return post(url, null);
    }
 
    public static String post(String url, Map<String, ? extends Object> params) throws Exception {
        return post(url, params, null);
    }
 
    public static String post(String url, Map<String, ? extends Object> params, Map<String, String> headers)
            throws Exception {
        if (url == null || url.trim().length() == 0) {
            throw new Exception(TAG + ":url is null or empty!");
        }
 
        if (params != null && params.size() > 0) {
            return tryToPost(url, buildParams(params), headers);
        } else {
            return tryToPost(url, null, headers);
        }
    }
 
    public static String post(String url, String content, Map<String, String> headers) throws Exception {
        return tryToPost(url, content, headers);
    }
 
    private static String tryToPost(String url, String postContent, Map<String, String> headers) throws Exception {
        int tryTime = 0;
        Exception ex = null;
        while (tryTime < mRetry) {
            try {
                return doPost(url, postContent, headers);
            } catch (Exception e) {
                if (e != null)
                    ex = e;
                tryTime++;
            }
        }
        if (ex != null)
            throw ex;
        else
            throw new Exception("未知网络错误 ");
    }
 
    private static String doPost(String strUrl, String postContent, Map<String, String> headers) throws Exception {
        HttpURLConnection connection = null;
        InputStream stream = null;
        try {
            connection = getConnection(strUrl);
            configConnection(connection);
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
 
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
 
            if (null != postContent && !"".equals(postContent)) {
                DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
                dos.write(postContent.getBytes(CHAR_SET));
                dos.flush();
                dos.close();
            }
            stream = connection.getInputStream();
            ByteArrayOutputStream obs = new ByteArrayOutputStream();
 
            byte[] buffer = new byte[1024];
            for (int len = 0; (len = stream.read(buffer)) > 0;) {
                obs.write(buffer, 0, len);
            }
            obs.flush();
            obs.close();
 
            return new String(obs.toByteArray());
 
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            if (stream != null) {
                stream.close();
            }
        }
 
    }
 
    private static void configConnection(HttpURLConnection connection) {
        if (connection == null)
            return;
        connection.setReadTimeout(mReadTimeOut);
        connection.setConnectTimeout(mConnectTimeOut);
 
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
    }
 
    private static HttpURLConnection getConnection(String strUrl) throws Exception {
        if (strUrl == null) {
            return null;
        }
        if (strUrl.toLowerCase().startsWith("https")) {
            return getHttpsConnection(strUrl);
        } else {
            return getHttpConnection(strUrl);
        }
    }
 
    private static HttpURLConnection getHttpConnection(String urlStr) throws Exception {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        return conn;
    }
 
    private static HttpsURLConnection getHttpsConnection(String urlStr) throws Exception {
        URL url = new URL(urlStr);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setHostnameVerifier(hnv);
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        if (sslContext != null) {
            TrustManager[] tm = { xtm };
            sslContext.init(null, tm, null);
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            conn.setSSLSocketFactory(ssf);
        }
 
        return conn;
    }
 
    private static X509TrustManager xtm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }
 
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }
 
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
 
    private static HostnameVerifier hnv = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
 
}