admin
2019-02-18 69ca458784b49c11b33dbee404ee242b4fb0b9c2
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package org.fanli.facade.system.util.taobao;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
import com.yeshi.fanli.entity.address.Address;
import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
 
import net.sf.json.JSONArray;
 
public class TaoBaoHttpUtil {
 
    public static String getShortLink(String url) {
        String totalUrl = String.format("http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=%s",
                URLEncoder.encode(url));
        String result = get(totalUrl);
        JSONArray rtesultArray = JSONArray.fromObject(result);
        if (rtesultArray.size() > 0) {
            return rtesultArray.optJSONObject(0).optString("url_short");
        }
        return null;
    }
 
    public static String proxyGet(String url, String cookie) {
        HttpClient client = new HttpClient();
        // Address address = ProxyUtil.getAddressProxy();
        // if(address != null){
        // client.getHostConfiguration().setProxy(address.getIp(),
        // address.getPort());
        // }
        try {
            GetMethod method = new GetMethod(url);
            if (cookie != null) {
                method.addRequestHeader("cookie", cookie);
            }
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String proxyGet(String url, Map<String, String> headers) {
        HttpClient client = new HttpClient();
        // Address address = ProxyUtil.getAddressProxy();
        // if(address != null){
        // client.getHostConfiguration().setProxy(address.getIp(),
        // address.getPort());
        // }
        try {
            GetMethod method = new GetMethod(url);
            if (headers != null) {
                Iterator<String> keys = headers.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    method.addRequestHeader(key, headers.get(key));
                }
            }
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String get(String url) {
        return get(url, true);
    }
 
    public static String get(String url, boolean isProxy) {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        if (isProxy) {
            Address address = ProxyUtil.getAddressProxy();
            if (address != null) {
                client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
            }
        }
        try {
            client.executeMethod(method);
            return convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String getAsString(String url, boolean isProxy) {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        if (isProxy) {
            Address address = ProxyUtil.getAddressProxy();
            if (address != null) {
                client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
            }
        }
        try {
            client.executeMethod(method);
            String response = method.getResponseBodyAsString();
            String s1 = new String(response.getBytes("ISO-8859-1"), "UTF-8");
            return s1;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String tbGet(String url, Map<String, String> params, String tbAccount) {
        HttpClient client = new HttpClient();
        Address address = ProxyUtil.getAddressProxy();
        // if(address != null){
        // client.getHostConfiguration().setProxy(address.getIp(),
        // address.getPort());
        // }
        Iterator<String> keys = params.keySet().iterator();
        url += "?";
        while (keys.hasNext()) {
            String key = keys.next();
            try {
                url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        GetMethod method = new GetMethod(url);
        try {
            method.setRequestHeader("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
            method.setRequestHeader("Upgrade-Insecure-Requests", "1");
            method.setRequestHeader("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            method.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
            method.setRequestHeader("Cache-Control", "max-age=0");
            String taoBaoCookie = TaoBaoOrderUtil.getTaoBaoCookie(tbAccount);
            method.setRequestHeader("cookie", taoBaoCookie);
            // HttpConnectionManagerParams params2 = new
            // HttpConnectionManagerParams();
            // params2.setConnectionTimeout(3500);
            // client.getHttpConnectionManager().setParams(params2);
            client.executeMethod(method);
            // InputStream inputStream = method.getResponseBodyAsStream();
            // String result = convertInputStreamToString(inputStream);
            return method.getResponseBodyAsString();// convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (Exception e) {
            try {
                address = ProxyUtil.getAddressProxy();
                if (address != null) {
                    client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
                }
                client.executeMethod(method);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return "";
    }
 
    public static String get(String url, Map<String, String> params, boolean proxy) {
        HttpClient client = new HttpClient();
        // client.getHostConfiguration().setProxy("192.168.1.122", 8888);
        try {
            Iterator<String> keys = params.keySet().iterator();
            url += "?";
            while (keys.hasNext()) {
                String key = keys.next();
                url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8"));
            }
 
            // System.out.println("url"+url);
            GetMethod method = new GetMethod(url);
            // 3S的响应超时
            HttpConnectionManagerParams hparams = new HttpConnectionManagerParams();
            hparams.setConnectionTimeout(3000);
            client.getHttpConnectionManager().setParams(hparams);
 
            /*
             * if(proxy){ Address address = ProxyUtil.getAddressProxy();
             * if(address != null){ HttpConnectionManagerParams hparams = new
             * HttpConnectionManagerParams();
             * hparams.setConnectionTimeout(3500);
             * client.getHttpConnectionManager().setParams(hparams);
             * client.getHostConfiguration().setProxy(address.getIp(),address.
             * getPort()); } }
             */
            // client.getHostConfiguration().setProxy("192.168.1.122",8888);
            client.executeMethod(method);
            String result = method.getResponseBodyAsString();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String get(String url, Map<String, String> params, Map<String, String> headers, boolean proxy) {
        HttpClient client = new HttpClient();
        try {
            Iterator<String> keys = params.keySet().iterator();
            url += "?";
            while (keys.hasNext()) {
                String key = keys.next();
                url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8"));
            }
 
            System.out.println(url);
 
            GetMethod method = new GetMethod(url);
            if (headers != null) {
                keys = headers.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    headers.get(key);
                    method.setRequestHeader(key, headers.get(key));
                }
            }
 
            /*
             * if(proxy){ Address address = ProxyUtil.getAddressProxy();
             * if(address != null){ HttpConnectionManagerParams hparams = new
             * HttpConnectionManagerParams();
             * hparams.setConnectionTimeout(3500);
             * client.getHttpConnectionManager().setParams(hparams);
             * client.getHostConfiguration().setProxy(address.getIp(),address.
             * getPort()); } }
             */
            // client.getHostConfiguration().setProxy("120.92.118.64", 10000);
 
            client.executeMethod(method);
            String result = method.getResponseBodyAsString();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String taoKeGet(Map<String, String> params) {
        // 聚石塔服务器环境 118.178.179.189
 
        // result = get("http://118.178.179.189/taoke/", params, false);
        // if (StringUtil.isNullOrEmpty(result))
        //https://eco.taobao.com/router/rest    
        //http://gw.api.taobao.com/router/rest
        String result = get("https://eco.taobao.com/router/rest", params, false);
        return result;
    }
 
    public static String get(String url, Map<String, String> params) {
        // TODO 淘宝的所有请求需要走聚石塔
        // http://118.178.179.189/taoke/
        return get(url, params, false);
    }
 
    public static String get(String url, String charset) {
        HttpClient client = new HttpClient();
        Address address = ProxyUtil.getAddressProxy();
        if (address != null) {
            client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
        }
        GetMethod method = new GetMethod(url);
        try {
            client.executeMethod(method);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(method.getResponseBodyAsStream(), "ISO-8859-1"));
            String tmp = null;
            String htmlRet = "";
            while ((tmp = reader.readLine()) != null) {
                htmlRet += tmp + "\r\n";
            }
            return new String(htmlRet.getBytes(charset), "UTF-8");
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
 
    }
 
    public static InputStream getAsInputStream(String url) {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        try {
            client.executeMethod(method);
            return method.getResponseBodyAsStream();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static InputStream getImage(String url) {
        HttpClient client = new HttpClient();
        Address address = ProxyUtil.getAddressProxy();
        if (address != null) {
            client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
        }
        GetMethod method = new GetMethod(url);
        try {
            method.setRequestHeader("Content-Type", "image/jpeg");
            client.executeMethod(method);
            return method.getResponseBodyAsStream();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String post(String url) {
        HttpClient client = new HttpClient();
        Address address = ProxyUtil.getAddressProxy();
        if (address != null) {
            client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
        }
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        try {
            client.executeMethod(method);
            return convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String post(String url, Map<String, String> map) {
        Iterator<String> its = map.keySet().iterator();
        NameValuePair[] params = new NameValuePair[map.keySet().size()];
        int p = 0;
        while (its.hasNext()) {
            String key = its.next();
            NameValuePair np = new NameValuePair(key, map.get(key));
            params[p] = np;
            p++;
        }
 
        HttpClient client = new HttpClient();
        Address address = ProxyUtil.getAddressProxy();
        if (address != null) {
            client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
        }
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestBody(params);
        try {
            client.executeMethod(method);
            return convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String post(String url, Map<String, String> map, Map<String, String> headers) {
        org.apache.http.client.HttpClient client = new DefaultHttpClient();
 
        HttpPost httpRequst = new HttpPost(url);// 创建HttpPost对象
        List<org.apache.http.NameValuePair> params = new ArrayList<org.apache.http.NameValuePair>();
        Iterator<String> its = map.keySet().iterator();
        while (its.hasNext()) {
            String key = its.next();
            params.add(new BasicNameValuePair(key, map.get(key)));
        }
        try {
            HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
            httpRequst.setEntity(entity);
            HttpResponse httpResponse = client.execute(httpRequst);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                String result = EntityUtils.toString(httpEntity, "UTF-8");// 取出应答字符�?
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return "";
 
        // Iterator<String> its = map.keySet().iterator();
    }
 
    private static String convertInputStreamToString(InputStream inputStream) {
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer stringBuffer = new StringBuffer();
        String str = "";
        try {
            while ((str = br.readLine()) != null) {
                stringBuffer.append(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
 
    @SuppressWarnings("deprecation")
    public static String post(String url, String entity) {
        HttpClient client = new HttpClient();
        Address address = ProxyUtil.getAddressProxy();
        if (address != null) {
            client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
        }
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestBody(entity);
        try {
            client.executeMethod(method);
            return convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String postInputStream(String url, File file) throws FileNotFoundException {
        HttpClient client = new HttpClient();
        Address address = ProxyUtil.getAddressProxy();
        if (address != null) {
            client.getHostConfiguration().setProxy(address.getIp(), address.getPort());
        }
        PostMethod postMethod = new PostMethod(url);
        /*
         * postMethod.addRequestHeader("Content-Type",
         * "text/html;charset=UTF-8");
         * postMethod.setRequestHeader("Content-Type",
         * "text/html;charset=UTF-8");
         */
        Part[] parts = { new StringPart("filename", file.getName()), new StringPart("filelength", file.length() + ""),
                new StringPart("content-type", "image/jpg"), new FilePart("file", file) };
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
 
        try {
            client.executeMethod(postMethod);
            return convertInputStreamToString(postMethod.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}