Skip to content

Commit bd2bfa9

Browse files
committed
增加ToValidator方法
1 parent 021e84e commit bd2bfa9

File tree

3 files changed

+58
-35
lines changed

3 files changed

+58
-35
lines changed

Demo/Program.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ static void Main(string[] args)
2828
/// </summary>
2929
static async void ScanProxy()
3030
{
31+
var timeout = TimeSpan.FromMilliseconds(500d);
3132
var target = new Uri("http://www.baidu.com");
32-
var proxys = HttpProxy.Range(IPAddress.Parse("221.122.13.1"), 8080, 9999);
3333

34-
var tasks = proxys.Select(async p =>
34+
var proxyValidators = HttpProxy
35+
.Range(IPAddress.Parse("221.122.13.1"), 8080, 9999)
36+
.Select(p => p.ToValidator());
37+
38+
var tasks = proxyValidators.Select(async v =>
3539
{
3640
Interlocked.Increment(ref totalProxyCount);
37-
if (await ProxyValidator.ValidateAsync(p, target, TimeSpan.FromMilliseconds(500d)) == HttpStatusCode.OK)
41+
if (await v.ValidateAsync(target, timeout) == HttpStatusCode.OK)
3842
{
39-
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}:扫描到代理服务:{p}");
43+
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}:扫描到代理服务:{v}");
4044
}
4145
var completed = Interlocked.Increment(ref completedProxyCount);
4246
Console.Title = $"代理扫描进度:{completed}/{Interlocked.Read(ref totalProxyCount)}";

WebApiClient/HttpProxy.cs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,32 +131,6 @@ private void SetCredentialsByInterface(ICredentials value)
131131
this.credentials = value;
132132
}
133133

134-
/// <summary>
135-
/// 从IWebProxy实例转换获得
136-
/// </summary>
137-
/// <param name="webProxy">IWebProxy</param>
138-
/// <param name="targetAddress">目标url地址</param>
139-
/// <exception cref="ArgumentNullException"></exception>
140-
/// <returns></returns>
141-
public static HttpProxy FromWebProxy(IWebProxy webProxy, Uri targetAddress)
142-
{
143-
if (webProxy == null)
144-
{
145-
throw new ArgumentNullException(nameof(webProxy));
146-
}
147-
148-
if (targetAddress == null)
149-
{
150-
throw new ArgumentNullException(nameof(targetAddress));
151-
}
152-
153-
var proxyAddress = webProxy.GetProxy(targetAddress);
154-
var httpProxy = new HttpProxy(proxyAddress);
155-
httpProxy.SetCredentialsByInterface(webProxy.Credentials);
156-
157-
return httpProxy;
158-
}
159-
160134
/// <summary>
161135
/// 转换Http Tunnel请求字符串
162136
/// </summary>
@@ -217,6 +191,42 @@ public override string ToString()
217191
return $"http://{this.Host}:{this.Port}/";
218192
}
219193

194+
/// <summary>
195+
/// 转换为代理验证器
196+
/// </summary>
197+
/// <returns></returns>
198+
public ProxyValidator ToValidator()
199+
{
200+
return new ProxyValidator(this);
201+
}
202+
203+
/// <summary>
204+
/// 从IWebProxy实例转换获得
205+
/// </summary>
206+
/// <param name="webProxy">IWebProxy</param>
207+
/// <param name="targetAddress">目标url地址</param>
208+
/// <exception cref="ArgumentNullException"></exception>
209+
/// <returns></returns>
210+
public static HttpProxy FromWebProxy(IWebProxy webProxy, Uri targetAddress)
211+
{
212+
if (webProxy == null)
213+
{
214+
throw new ArgumentNullException(nameof(webProxy));
215+
}
216+
217+
if (targetAddress == null)
218+
{
219+
throw new ArgumentNullException(nameof(targetAddress));
220+
}
221+
222+
var proxyAddress = webProxy.GetProxy(targetAddress);
223+
var httpProxy = new HttpProxy(proxyAddress);
224+
httpProxy.SetCredentialsByInterface(webProxy.Credentials);
225+
226+
return httpProxy;
227+
}
228+
229+
220230
/// <summary>
221231
/// 指定ip范围构建http代理服务
222232
/// </summary>

WebApiClient/ProxyValidator.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace WebApiClient
1515
public class ProxyValidator
1616
{
1717
/// <summary>
18-
/// 代理
18+
/// 获取代理
1919
/// </summary>
20-
private readonly IWebProxy webProxy;
20+
public IWebProxy WebProxy { get; private set; }
2121

2222
/// <summary>
2323
/// 代理验证器
@@ -37,7 +37,7 @@ public ProxyValidator(string proxyHost, int proxyPort)
3737
/// <exception cref="ArgumentNullException"></exception>
3838
public ProxyValidator(IWebProxy webProxy)
3939
{
40-
this.webProxy = webProxy ?? throw new ArgumentNullException(nameof(webProxy));
40+
this.WebProxy = webProxy ?? throw new ArgumentNullException(nameof(webProxy));
4141
}
4242

4343
/// <summary>
@@ -53,7 +53,7 @@ public HttpStatusCode Validate(Uri targetAddress, TimeSpan? timeout = null)
5353
{
5454
throw new ArgumentNullException(nameof(targetAddress));
5555
}
56-
return Validate(this.webProxy, targetAddress, timeout);
56+
return Validate(this.WebProxy, targetAddress, timeout);
5757
}
5858

5959
/// <summary>
@@ -69,7 +69,16 @@ public Task<HttpStatusCode> ValidateAsync(Uri targetAddress, TimeSpan? timeout =
6969
{
7070
throw new ArgumentNullException(nameof(targetAddress));
7171
}
72-
return ValidateAsync(this.webProxy, targetAddress, timeout);
72+
return ValidateAsync(this.WebProxy, targetAddress, timeout);
73+
}
74+
75+
/// <summary>
76+
/// 转换为字符串
77+
/// </summary>
78+
/// <returns></returns>
79+
public override string ToString()
80+
{
81+
return this.WebProxy.ToString();
7382
}
7483

7584
/// <summary>

0 commit comments

Comments
 (0)