admin
2022-01-12 c9fdff7d45ae118eac10397d8f34661e9f2e968a
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
package com.ks.lijin.utils.taobao;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 
 
public class TaoBaoHttpUtil {
 
 
    public static String proxyGet(String url, String cookie) {
        HttpClient client = new HttpClient();
        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();
        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, 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) {
        String result = get("https://eco.taobao.com/router/rest", params, false);
        return result;
    }
 
    public static String get(String url, Map<String, String> params) {
        return get(url, params, false);
    }
 
    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;
    }
 
 
 
 
 
 
 
 
    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();
    }
 
 
 
}