办学质量监测教学评价系统
ageer
2024-02-27 c84894c5a5393ee031a71f45e05a084096745ee5
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
package com.xmzs.common.wechat.utils;
 
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Logger;
 
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
/**
 * HTTP访问类,对Apache HttpClient进行简单封装,适配器模式
 *
 * @author https://github.com/yaphone
 * @date 创建时间:2017年4月9日 下午7:05:04
 * @version 1.0
 *
 */
public class MyHttpClient {
    private Logger logger = Logger.getLogger("MyHttpClient");
 
    private static CloseableHttpClient httpClient = HttpClients.createDefault();
 
    private static MyHttpClient instance = null;
 
    private static CookieStore cookieStore;
 
    static {
        cookieStore = new BasicCookieStore();
 
        // 将CookieStore设置到httpClient中
        httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    }
 
    public static String getCookie(String name) {
        List<Cookie> cookies = cookieStore.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equalsIgnoreCase(name)) {
                return cookie.getValue();
            }
        }
        return null;
 
    }
 
    private MyHttpClient() {
 
    }
 
    /**
     * 获取cookies
     *
     * @author https://github.com/yaphone
     * @date 2017年5月7日 下午8:37:17
     * @return
     */
    public static MyHttpClient getInstance() {
        if (instance == null) {
            synchronized (MyHttpClient.class) {
                if (instance == null) {
                    instance = new MyHttpClient();
                }
            }
        }
        return instance;
    }
 
    /**
     * 处理GET请求
     *
     * @author https://github.com/yaphone
     * @date 2017年4月9日 下午7:06:19
     * @param url
     * @param params
     * @return
     */
    public HttpEntity doGet(String url, List<BasicNameValuePair> params, boolean redirect,
            Map<String, String> headerMap) {
        HttpEntity entity = null;
        HttpGet httpGet = new HttpGet();
 
        try {
            if (params != null) {
                String paramStr = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
                httpGet = new HttpGet(url + "?" + paramStr);
            } else {
                httpGet = new HttpGet(url);
            }
            if (!redirect) {
                httpGet.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build()); // 禁止重定向
            }
            httpGet.setHeader("User-Agent", Config.USER_AGENT);
            httpGet.setHeader("client-version", Config.UOS_PATCH_CLIENT_VERSION);
            httpGet.setHeader("extspam", Config.UOS_PATCH_EXTSPAM);
            httpGet.setHeader("referer", Config.REFERER);
            if (headerMap != null) {
                Set<Entry<String, String>> entries = headerMap.entrySet();
                for (Entry<String, String> entry : entries) {
                    httpGet.setHeader(entry.getKey(), entry.getValue());
                }
            }
            CloseableHttpResponse response = httpClient.execute(httpGet);
            entity = response.getEntity();
        } catch (ClientProtocolException e) {
            logger.info(e.getMessage());
        } catch (IOException e) {
            logger.info(e.getMessage());
        }
 
        return entity;
    }
 
    /**
     * 处理POST请求
     *
     * @author https://github.com/yaphone
     * @date 2017年4月9日 下午7:06:35
     * @param url
     * @param params
     * @return
     */
    public HttpEntity doPost(String url, String paramsStr) {
        HttpEntity entity = null;
        HttpPost httpPost = new HttpPost();
        try {
            StringEntity params = new StringEntity(paramsStr, Consts.UTF_8);
            httpPost = new HttpPost(url);
            httpPost.setEntity(params);
            httpPost.setHeader("Content-type", "application/json; charset=utf-8");
            httpPost.setHeader("User-Agent", Config.USER_AGENT);
            httpPost.setHeader("client-version", Config.UOS_PATCH_CLIENT_VERSION);
            httpPost.setHeader("extspam", Config.UOS_PATCH_EXTSPAM);
            httpPost.setHeader("referer", Config.REFERER);
 
            CloseableHttpResponse response = httpClient.execute(httpPost);
            entity = response.getEntity();
        } catch (ClientProtocolException e) {
            logger.info(e.getMessage());
        } catch (IOException e) {
            logger.info(e.getMessage());
        }
 
        return entity;
    }
 
    /**
     * 上传文件到服务器
     *
     * @author https://github.com/yaphone
     * @date 2017年5月7日 下午9:19:23
     * @param url
     * @param reqEntity
     * @return
     */
    public HttpEntity doPostFile(String url, HttpEntity reqEntity) {
        HttpEntity entity = null;
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("User-Agent", Config.USER_AGENT);
        httpPost.setHeader("client-version", Config.UOS_PATCH_CLIENT_VERSION);
        httpPost.setHeader("extspam", Config.UOS_PATCH_EXTSPAM);
        httpPost.setHeader("referer", Config.REFERER);
 
        httpPost.setEntity(reqEntity);
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            entity = response.getEntity();
 
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        return entity;
    }
 
    public static CloseableHttpClient getHttpClient() {
        return httpClient;
    }
 
}