11using System ;
2- using System . Collections . Generic ;
3- using System . Linq ;
42using System . Net ;
53using System . Text ;
6- using System . Threading . Tasks ;
74
85namespace WebApiClient
96{
107 /// <summary>
11- /// 表示代理信息
8+ /// 表示http代理信息
129 /// </summary>
13- public class ProxyInfo
10+ public class HttpProxy : IWebProxy
1411 {
1512 /// <summary>
1613 /// 获取代理服务器域名或ip
@@ -25,32 +22,37 @@ public class ProxyInfo
2522 /// <summary>
2623 /// 获取代理服务器账号
2724 /// </summary>
28- public string UserName { get ; set ; }
25+ public string UserName { get ; private set ; }
2926
3027 /// <summary>
3128 /// 获取代理服务器密码
3229 /// </summary>
33- public string Password { get ; set ; }
30+ public string Password { get ; private set ; }
3431
3532 /// <summary>
36- /// 代理信息
33+ /// 获取或设置授权信息
34+ /// </summary>
35+ ICredentials IWebProxy . Credentials { get ; set ; }
36+
37+ /// <summary>
38+ /// http代理信息
3739 /// </summary>
3840 /// <param name="proxyAddress">代理服务器地址</param>
3941 /// <exception cref="ArgumentNullException"></exception>
4042 /// <exception cref="ArgumentException"></exception>
4143 /// <exception cref="ArgumentOutOfRangeException"></exception>
4244 /// <exception cref="UriFormatException"></exception>
43- public ProxyInfo ( string proxyAddress )
45+ public HttpProxy ( string proxyAddress )
4446 : this ( new Uri ( proxyAddress ?? throw new ArgumentNullException ( nameof ( proxyAddress ) ) ) )
4547 {
4648 }
4749
4850 /// <summary>
49- /// 代理信息
51+ /// http代理信息
5052 /// </summary>
5153 /// <param name="proxyAddress">代理服务器地址</param>
5254 /// <exception cref="ArgumentNullException"></exception>
53- public ProxyInfo ( Uri proxyAddress )
55+ public HttpProxy ( Uri proxyAddress )
5456 {
5557 if ( proxyAddress == null )
5658 {
@@ -61,15 +63,35 @@ public ProxyInfo(Uri proxyAddress)
6163 }
6264
6365 /// <summary>
64- /// 代理信息
66+ /// http代理信息
67+ /// </summary>
68+ /// <param name="host">代理服务器域名或ip</param>
69+ /// <param name="port">代理服务器端口</param>
70+ /// <exception cref="ArgumentNullException"></exception>
71+ public HttpProxy ( string host , int port )
72+ : this ( host , port , null , null )
73+ {
74+ }
75+
76+ /// <summary>
77+ /// http代理信息
6578 /// </summary>
6679 /// <param name="host">代理服务器域名或ip</param>
6780 /// <param name="port">代理服务器端口</param>
81+ /// <param name="userName">代理服务器账号</param>
82+ /// <param name="password">代理服务器密码</param>
6883 /// <exception cref="ArgumentNullException"></exception>
69- public ProxyInfo ( string host , int port )
84+ public HttpProxy ( string host , int port , string userName , string password )
7085 {
7186 this . Host = host ?? throw new ArgumentNullException ( nameof ( host ) ) ;
72- this . Port = Port ;
87+ this . Port = port ;
88+ this . UserName = userName ;
89+ this . Password = password ;
90+
91+ if ( string . IsNullOrEmpty ( userName + password ) == false )
92+ {
93+ ( ( IWebProxy ) this ) . Credentials = new NetworkCredential ( userName , password ) ;
94+ }
7395 }
7496
7597 /// <summary>
@@ -79,7 +101,7 @@ public ProxyInfo(string host, int port)
79101 /// <param name="targetAddress">目标url地址</param>
80102 /// <exception cref="ArgumentNullException"></exception>
81103 /// <returns></returns>
82- public static ProxyInfo FromWebProxy ( IWebProxy webProxy , Uri targetAddress )
104+ public static HttpProxy FromWebProxy ( IWebProxy webProxy , Uri targetAddress )
83105 {
84106 if ( webProxy == null )
85107 {
@@ -92,15 +114,15 @@ public static ProxyInfo FromWebProxy(IWebProxy webProxy, Uri targetAddress)
92114 }
93115
94116 var proxyAddress = webProxy . GetProxy ( targetAddress ) ;
95- var proxyInfo = new ProxyInfo ( proxyAddress ) ;
117+ var httpProxy = new HttpProxy ( proxyAddress ) ;
96118
97119 if ( webProxy . Credentials != null )
98120 {
99121 var credentials = webProxy . Credentials . GetCredential ( null , null ) ;
100- proxyInfo . UserName = credentials ? . UserName ;
101- proxyInfo . Password = credentials ? . Password ;
122+ httpProxy . UserName = credentials ? . UserName ;
123+ httpProxy . Password = credentials ? . Password ;
102124 }
103- return proxyInfo ;
125+ return httpProxy ;
104126 }
105127
106128 /// <summary>
@@ -109,7 +131,7 @@ public static ProxyInfo FromWebProxy(IWebProxy webProxy, Uri targetAddress)
109131 /// <param name="targetAddress">目标url地址</param>
110132 /// <exception cref="ArgumentNullException"></exception>
111133 /// <returns></returns>
112- public string ToHttpTunnelRequestString ( Uri targetAddress )
134+ public string ToTunnelRequestString ( Uri targetAddress )
113135 {
114136 if ( targetAddress == null )
115137 {
@@ -135,15 +157,23 @@ public string ToHttpTunnelRequestString(Uri targetAddress)
135157 }
136158
137159 /// <summary>
138- /// 转换为web代理
160+ /// 获取代理服务器地址
139161 /// </summary>
162+ /// <param name="destination"></param>
140163 /// <returns></returns>
141- public IWebProxy ToWebProxy ( )
164+ Uri IWebProxy . GetProxy ( Uri destination )
142165 {
143- return new WebProxy ( this . Host , this . Port )
144- {
145- Credentials = new NetworkCredential ( this . UserName , this . Password )
146- } ;
166+ return new Uri ( $ "http://{ this . Host } :{ this . Port } /") ;
167+ }
168+
169+ /// <summary>
170+ /// 是否忽略代理
171+ /// </summary>
172+ /// <param name="host"></param>
173+ /// <returns></returns>
174+ bool IWebProxy . IsBypassed ( Uri host )
175+ {
176+ return false ;
147177 }
148178 }
149179}
0 commit comments