@@ -9,6 +9,11 @@ namespace WebApiClient
99 /// </summary>
1010 public class HttpProxy : IWebProxy
1111 {
12+ /// <summary>
13+ /// 授权字段
14+ /// </summary>
15+ private ICredentials credentials ;
16+
1217 /// <summary>
1318 /// 获取代理服务器域名或ip
1419 /// </summary>
@@ -32,7 +37,17 @@ public class HttpProxy : IWebProxy
3237 /// <summary>
3338 /// 获取或设置授权信息
3439 /// </summary>
35- ICredentials IWebProxy . Credentials { get ; set ; }
40+ ICredentials IWebProxy . Credentials
41+ {
42+ get
43+ {
44+ return this . credentials ;
45+ }
46+ set
47+ {
48+ this . SetCredentialsByInterface ( value ) ;
49+ }
50+ }
3651
3752 /// <summary>
3853 /// http代理信息
@@ -69,8 +84,9 @@ public HttpProxy(Uri proxyAddress)
6984 /// <param name="port">代理服务器端口</param>
7085 /// <exception cref="ArgumentNullException"></exception>
7186 public HttpProxy ( string host , int port )
72- : this ( host , port , null , null )
7387 {
88+ this . Host = host ?? throw new ArgumentNullException ( nameof ( host ) ) ;
89+ this . Port = port ;
7490 }
7591
7692 /// <summary>
@@ -82,18 +98,37 @@ public HttpProxy(string host, int port)
8298 /// <param name="password">代理服务器密码</param>
8399 /// <exception cref="ArgumentNullException"></exception>
84100 public HttpProxy ( string host , int port , string userName , string password )
101+ : this ( host , port )
85102 {
86- this . Host = host ?? throw new ArgumentNullException ( nameof ( host ) ) ;
87- this . Port = port ;
88103 this . UserName = userName ;
89104 this . Password = password ;
90105
91106 if ( string . IsNullOrEmpty ( userName + password ) == false )
92107 {
93- ( ( IWebProxy ) this ) . Credentials = new NetworkCredential ( userName , password ) ;
108+ this . credentials = new NetworkCredential ( userName , password ) ;
94109 }
95110 }
96111
112+ /// <summary>
113+ /// 通过接口设置授权信息
114+ /// </summary>
115+ /// <param name="value"></param>
116+ private void SetCredentialsByInterface ( ICredentials value )
117+ {
118+ var userName = default ( string ) ;
119+ var password = default ( string ) ;
120+ if ( value != null )
121+ {
122+ var networkCredentialsd = value . GetCredential ( null , null ) ;
123+ userName = networkCredentialsd ? . UserName ;
124+ password = networkCredentialsd ? . Password ;
125+ }
126+
127+ this . UserName = userName ;
128+ this . Password = password ;
129+ this . credentials = value ;
130+ }
131+
97132 /// <summary>
98133 /// 从IWebProxy实例转换获得
99134 /// </summary>
@@ -115,13 +150,8 @@ public static HttpProxy FromWebProxy(IWebProxy webProxy, Uri targetAddress)
115150
116151 var proxyAddress = webProxy . GetProxy ( targetAddress ) ;
117152 var httpProxy = new HttpProxy ( proxyAddress ) ;
153+ httpProxy . SetCredentialsByInterface ( webProxy . Credentials ) ;
118154
119- if ( webProxy . Credentials != null )
120- {
121- var credentials = webProxy . Credentials . GetCredential ( null , null ) ;
122- httpProxy . UserName = credentials ? . UserName ;
123- httpProxy . Password = credentials ? . Password ;
124- }
125155 return httpProxy ;
126156 }
127157
@@ -159,19 +189,19 @@ public string ToTunnelRequestString(Uri targetAddress)
159189 /// <summary>
160190 /// 获取代理服务器地址
161191 /// </summary>
162- /// <param name="destination"></param>
192+ /// <param name="destination">目标地址 </param>
163193 /// <returns></returns>
164- Uri IWebProxy . GetProxy ( Uri destination )
194+ public Uri GetProxy ( Uri destination )
165195 {
166196 return new Uri ( $ "http://{ this . Host } :{ this . Port } /") ;
167197 }
168198
169199 /// <summary>
170200 /// 是否忽略代理
171201 /// </summary>
172- /// <param name="host"></param>
202+ /// <param name="host">目标地址 </param>
173203 /// <returns></returns>
174- bool IWebProxy . IsBypassed ( Uri host )
204+ public bool IsBypassed ( Uri host )
175205 {
176206 return false ;
177207 }
0 commit comments