|
1 | 1 | package com.shuzijun.leetcode.plugin.utils;
|
2 | 2 |
|
3 |
| -import com.intellij.util.net.HttpConfigurable; |
| 3 | +import com.intellij.util.proxy.CommonProxy; |
4 | 4 | import com.shuzijun.leetcode.plugin.model.Config;
|
5 | 5 | import com.shuzijun.leetcode.plugin.setting.PersistentConfig;
|
6 | 6 | import org.apache.commons.lang.StringUtils;
|
7 | 7 | import org.apache.http.Header;
|
8 | 8 | import org.apache.http.HttpHeaders;
|
9 |
| -import org.apache.http.HttpHost; |
10 |
| -import org.apache.http.auth.AuthScope; |
11 |
| -import org.apache.http.auth.UsernamePasswordCredentials; |
12 |
| -import org.apache.http.client.CredentialsProvider; |
13 | 9 | import org.apache.http.client.config.AuthSchemes;
|
14 | 10 | import org.apache.http.client.config.CookieSpecs;
|
15 | 11 | import org.apache.http.client.config.RequestConfig;
|
|
20 | 16 | import org.apache.http.client.protocol.HttpClientContext;
|
21 | 17 | import org.apache.http.config.Registry;
|
22 | 18 | import org.apache.http.config.RegistryBuilder;
|
| 19 | +import org.apache.http.conn.routing.HttpRoutePlanner; |
23 | 20 | import org.apache.http.conn.socket.ConnectionSocketFactory;
|
24 | 21 | import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
25 | 22 | import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
26 | 23 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
27 | 24 | import org.apache.http.cookie.Cookie;
|
28 |
| -import org.apache.http.impl.client.*; |
| 25 | +import org.apache.http.impl.client.BasicCookieStore; |
| 26 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 27 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 28 | +import org.apache.http.impl.client.HttpClients; |
29 | 29 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
| 30 | +import org.apache.http.impl.conn.SystemDefaultRoutePlanner; |
30 | 31 | import org.apache.http.impl.cookie.BasicClientCookie;
|
31 | 32 | import org.apache.http.message.BasicHeader;
|
32 | 33 |
|
@@ -61,29 +62,22 @@ private static void createHttpClient() {
|
61 | 62 | .setExpectContinueEnabled(Boolean.TRUE).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
|
62 | 63 | .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC));
|
63 | 64 |
|
64 |
| - //proxy |
65 |
| - Config config = PersistentConfig.getInstance().getInitConfig(); |
66 |
| - HttpConfigurable proxySettings = HttpConfigurable.getInstance(); |
67 |
| - CredentialsProvider provider = null; |
68 |
| - if (config != null && config.getProxy() && proxySettings != null && proxySettings.USE_HTTP_PROXY && !proxySettings.PROXY_TYPE_IS_SOCKS) { |
69 |
| - HttpHost proxy = new HttpHost(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT, "http"); |
70 |
| - globalConfigBuilder.setProxy(proxy); |
71 |
| - if (proxySettings.PROXY_AUTHENTICATION) { |
72 |
| - provider = new BasicCredentialsProvider(); |
73 |
| - provider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(proxySettings.getProxyLogin(), proxySettings.getPlainProxyPassword())); |
74 |
| - } |
75 |
| - |
76 |
| - } |
77 |
| - |
78 | 65 |
|
79 | 66 | HttpClientBuilder httpClientBuilder = HttpClients.custom()
|
80 | 67 | .setDefaultRequestConfig(globalConfigBuilder.build())
|
81 | 68 | .setDefaultCookieStore(cookieStore)
|
82 | 69 | .setDefaultHeaders(defaultHeader())
|
83 | 70 | .setConnectionManager(getconnectionManager());
|
84 |
| - if (provider != null) { |
85 |
| - httpClientBuilder.setDefaultCredentialsProvider(provider); |
| 71 | + |
| 72 | + //proxy |
| 73 | + Config config = PersistentConfig.getInstance().getInitConfig(); |
| 74 | + |
| 75 | + if (config != null && config.getProxy()) { |
| 76 | + CommonProxy commonProxy = CommonProxy.getInstance(); |
| 77 | + HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(commonProxy); |
| 78 | + httpClientBuilder.setRoutePlanner(routePlanner); |
86 | 79 | }
|
| 80 | + |
87 | 81 | httpclient = httpClientBuilder.build();
|
88 | 82 |
|
89 | 83 | }
|
@@ -127,7 +121,7 @@ public static CloseableHttpResponse execute(HttpUriRequest httpUriRequest) {
|
127 | 121 | CloseableHttpResponse response = httpclient.execute(httpUriRequest, context);
|
128 | 122 | return response;
|
129 | 123 | } catch (Exception e) {
|
130 |
| - LogUtils.LOG.error("请求出错:", e); |
| 124 | + LogUtils.LOG.error("httpclient request error:", e); |
131 | 125 | } finally {
|
132 | 126 | }
|
133 | 127 |
|
|
0 commit comments