44import java .io .IOException ;
55import java .io .InputStream ;
66import java .io .OutputStream ;
7+ import java .io .UnsupportedEncodingException ;
78import java .net .HttpURLConnection ;
9+ import java .net .InetSocketAddress ;
10+ import java .net .MalformedURLException ;
11+ import java .net .Proxy ;
12+ import java .net .SocketAddress ;
813import java .net .URL ;
914import java .security .KeyManagementException ;
1015import java .security .NoSuchAlgorithmException ;
2227import javax .net .ssl .SSLSocketFactory ;
2328import javax .net .ssl .TrustManager ;
2429import javax .net .ssl .X509TrustManager ;
30+ import javax .xml .bind .DatatypeConverter ;
2531
2632import com .aliyuncs .exceptions .ClientException ;
2733import com .aliyuncs .http .CallBack ;
@@ -94,7 +100,6 @@ public static HttpResponse compatibleGetResponse(HttpRequest request) throws IOE
94100 }
95101
96102 private HttpURLConnection buildHttpConnection (HttpRequest request ) throws IOException {
97- Map <String , String > mappedHeaders = request .getHeaders ();
98103 String strUrl = request .getUrl ();
99104
100105 if (null == strUrl ) {
@@ -115,14 +120,16 @@ private HttpURLConnection buildHttpConnection(HttpRequest request) throws IOExce
115120 HttpURLConnection httpConn = null ;
116121 if (url .getProtocol ().equalsIgnoreCase ("https" )) {
117122 if (sslSocketFactory != null ) {
118- HttpsURLConnection httpsConn = (HttpsURLConnection )url .openConnection ();
123+ Proxy proxy = getProxy ("HTTPS_PROXY" , request );
124+ HttpsURLConnection httpsConn = (HttpsURLConnection )url .openConnection (proxy );
119125 httpsConn .setSSLSocketFactory (sslSocketFactory );
120126 httpConn = httpsConn ;
121127 }
122128 }
123129
124130 if (httpConn == null ) {
125- httpConn = (HttpURLConnection )url .openConnection ();
131+ Proxy proxy = getProxy ("HTTP_PROXY" , request );
132+ httpConn = (HttpURLConnection )url .openConnection (proxy );
126133 }
127134
128135 httpConn .setRequestMethod (request .getMethod ().toString ());
@@ -138,6 +145,7 @@ private HttpURLConnection buildHttpConnection(HttpRequest request) throws IOExce
138145 httpConn .setReadTimeout (request .getReadTimeout ());
139146 }
140147
148+ Map <String , String > mappedHeaders = request .getHeaders ();
141149 httpConn .setRequestProperty (ACCEPT_ENCODING , "identity" );
142150 for (Entry <String , String > entry : mappedHeaders .entrySet ()) {
143151 httpConn .setRequestProperty (entry .getKey (), entry .getValue ());
@@ -222,6 +230,28 @@ public void close() throws IOException {
222230
223231 }
224232
233+ private Proxy getProxy (String env , HttpRequest request ) throws MalformedURLException , UnsupportedEncodingException {
234+ Proxy proxy = Proxy .NO_PROXY ;
235+ String httpProxy = System .getenv (env );
236+ if (httpProxy != null ) {
237+ URL proxyUrl = new URL (httpProxy );
238+ String userInfo = proxyUrl .getUserInfo ();
239+ if (userInfo != null ) {
240+ byte [] bytes = userInfo .getBytes ("UTF-8" );
241+ String auth = DatatypeConverter .printBase64Binary (bytes );
242+ request .putHeaderParameter ("Proxy-Authorization" , "Basic " + auth );
243+ }
244+ String hostname = proxyUrl .getHost ();
245+ int port = proxyUrl .getPort ();
246+ if (port == -1 ) {
247+ port = proxyUrl .getDefaultPort ();
248+ }
249+ SocketAddress addr = new InetSocketAddress (hostname , port );
250+ proxy = new Proxy (Proxy .Type .HTTP , addr );
251+ }
252+ return proxy ;
253+ }
254+
225255 public static final class HttpsCertIgnoreHelper implements X509TrustManager , HostnameVerifier {
226256
227257 private static HostnameVerifier defaultVerifier ;
0 commit comments