@@ -17,16 +17,48 @@ public class ProxyValidator
1717 /// <summary>
1818 /// 代理
1919 /// </summary>
20- private readonly IWebProxy proxy ;
20+ private readonly IWebProxy webProxy ;
21+
22+ /// <summary>
23+ /// 代理信息
24+ /// </summary>
25+ private readonly ProxyInfo proxyInfo ;
26+
27+ /// <summary>
28+ /// 代理验证器
29+ /// </summary>
30+ /// <param name="webProxy">代理</param>
31+ /// <exception cref="ArgumentNullException"></exception>
32+ public ProxyValidator ( IWebProxy webProxy )
33+ {
34+ this . webProxy = webProxy ?? throw new ArgumentNullException ( nameof ( webProxy ) ) ;
35+ }
2136
2237 /// <summary>
2338 /// 代理验证器
2439 /// </summary>
25- /// <param name="proxy">代理 </param>
40+ /// <param name="proxyInfo">代理信息 </param>
2641 /// <exception cref="ArgumentNullException"></exception>
27- public ProxyValidator ( IWebProxy proxy )
42+ public ProxyValidator ( ProxyInfo proxyInfo )
2843 {
29- this . proxy = proxy ?? throw new ArgumentNullException ( nameof ( proxy ) ) ;
44+ this . proxyInfo = proxyInfo ?? throw new ArgumentNullException ( nameof ( proxyInfo ) ) ;
45+ }
46+
47+ /// <summary>
48+ /// 获取代理信息
49+ /// </summary>
50+ /// <param name="targetAddress"></param>
51+ /// <returns></returns>
52+ private ProxyInfo GetProxy ( Uri targetAddress )
53+ {
54+ if ( this . webProxy != null )
55+ {
56+ return ProxyInfo . FromWebProxy ( this . webProxy , targetAddress ) ;
57+ }
58+ else
59+ {
60+ return this . proxyInfo ;
61+ }
3062 }
3163
3264 /// <summary>
@@ -43,7 +75,7 @@ public HttpStatusCode Validate(Uri targetAddress, TimeSpan timeout)
4375 throw new ArgumentNullException ( nameof ( targetAddress ) ) ;
4476 }
4577
46- var proxyInfo = ProxyInfo . FromWebProxy ( this . proxy , targetAddress ) ;
78+ var proxyInfo = this . GetProxy ( targetAddress ) ;
4779 return ProxyValidator . Validate ( proxyInfo , targetAddress , timeout ) ;
4880 }
4981
@@ -61,7 +93,7 @@ public Task<HttpStatusCode> ValidateAsync(Uri targetAddress, TimeSpan timeout)
6193 throw new ArgumentNullException ( nameof ( targetAddress ) ) ;
6294 }
6395
64- var proxyInfo = ProxyInfo . FromWebProxy ( this . proxy , targetAddress ) ;
96+ var proxyInfo = this . GetProxy ( targetAddress ) ;
6597 return ProxyValidator . ValidateAsync ( proxyInfo , targetAddress , timeout ) ;
6698 }
6799
0 commit comments