2727
2828import javax .net .ssl .SSLContext ;
2929
30+ import org .apache .commons .codec .binary .Base64 ;
3031import org .apache .http .Header ;
3132import org .apache .http .HttpHost ;
3233import org .apache .http .HttpStatus ;
34+ import org .apache .http .auth .AUTH ;
3335import org .apache .http .auth .AuthScope ;
3436import org .apache .http .auth .NTCredentials ;
37+ import org .apache .http .client .AuthCache ;
3538import org .apache .http .client .CredentialsProvider ;
3639import org .apache .http .client .config .RequestConfig ;
3740import org .apache .http .client .methods .CloseableHttpResponse ;
4649import org .apache .http .conn .ssl .NoopHostnameVerifier ;
4750import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
4851import org .apache .http .conn .ssl .TrustStrategy ;
52+ import org .apache .http .impl .auth .BasicScheme ;
53+ import org .apache .http .impl .client .BasicAuthCache ;
4954import org .apache .http .impl .client .BasicCredentialsProvider ;
5055import org .apache .http .impl .client .CloseableHttpClient ;
5156import org .apache .http .impl .client .HttpClients ;
@@ -73,6 +78,7 @@ public class DefaultServiceClient extends ServiceClient {
7378 protected RequestConfig requestConfig ;
7479 protected CredentialsProvider credentialsProvider ;
7580 protected HttpHost proxyHttpHost ;
81+ protected AuthCache authCache ;
7682
7783 public DefaultServiceClient (ClientConfiguration config ) {
7884 super (config );
@@ -97,6 +103,9 @@ public DefaultServiceClient(ClientConfiguration config) {
97103 this .credentialsProvider = new BasicCredentialsProvider ();
98104 this .credentialsProvider .setCredentials (new AuthScope (proxyHost , proxyPort ),
99105 new NTCredentials (proxyUsername , proxyPassword , proxyWorkstation , proxyDomain ));
106+
107+ this .authCache = new BasicAuthCache ();
108+ authCache .put (this .proxyHttpHost , new BasicScheme ());
100109 }
101110 }
102111
@@ -107,7 +116,8 @@ public DefaultServiceClient(ClientConfiguration config) {
107116 public ResponseMessage sendRequestCore (ServiceClient .Request request , ExecutionContext context )
108117 throws IOException {
109118 HttpRequestBase httpRequest = httpRequestFactory .createHttpRequest (request , context );
110- HttpClientContext httpContext = HttpClientContext .create ();
119+ setProxyAuthorizationIfNeed (httpRequest );
120+ HttpClientContext httpContext = createHttpContext ();
111121 httpContext .setRequestConfig (this .requestConfig );
112122
113123 CloseableHttpResponse httpResponse = null ;
@@ -255,9 +265,19 @@ protected HttpClientContext createHttpContext() {
255265 httpContext .setRequestConfig (this .requestConfig );
256266 if (this .credentialsProvider != null ) {
257267 httpContext .setCredentialsProvider (this .credentialsProvider );
268+ httpContext .setAuthCache (this .authCache );
258269 }
259270 return httpContext ;
260271 }
272+
273+ private void setProxyAuthorizationIfNeed (HttpRequestBase httpRequest ) {
274+ if (this .credentialsProvider != null ) {
275+ String auth = this .config .getProxyUsername () + ":" + this .config .getProxyPassword ();
276+ byte [] encodedAuth = Base64 .encodeBase64 (auth .getBytes ());
277+ String authHeader = "Basic " + new String (encodedAuth );
278+ httpRequest .addHeader (AUTH .PROXY_AUTH_RESP , authHeader );
279+ }
280+ }
261281
262282 @ Override
263283 public void shutdown () {
0 commit comments