C#全新。
我正在使用TPL测试代理服务器列表的有效性。目前我只使用localhost + Fiddler进行测试。 Task.WhenAll在我的任务列表实际完成之前完成。我在SO上发现a similar question但不幸的是,接受的答案是不适用于我的案例的,因为我没有像那个问题的OP那样调用异步无效方法。
致电代码:
public class Proxy
{
public string IP { get; set; }
public int Port { get; set; }
public bool IsValid { get; set; }
public Proxy(string ip, int port)
{
//TODO Validate input.
this.IP = ip;
this.Port = port;
}
public async Task<bool> TestValidityAsync(ProxyTest test)
{
try
{
var req = HttpWebRequest.Create(test.URL);
req.Proxy = new WebProxy(this.ToString());
var resp = await req.GetResponseAsync();
using (var sr = new StreamReader(resp.GetResponseStream()))
{
var respBody = await sr.ReadToEndAsync();
if (respBody.Contains(test.Validation))
{
return true;
}
else
{
return false;
}
}
}
catch (Exception)
{
return false;
}
}
public override string ToString()
{
return this.IP + ":" + this.Port;
}
}
public class ProxyTest
{
public string URL { get; set; }
public string Validation { get; set; }
public string Invalidation { get; set; }
public ProxyTest(string url, string validation)
{
this.URL = url;
this.Validation = validation;
}
public ProxyTest(string url, string validation, string invalidation)
{
this.URL = url;
this.Validation = validation;
this.Invalidation = invalidation;
}
}
相关课程:
interleave (x:xs) (y:ys) = x : y : interleave xs ys
interleave [] ys = ys
interleave xs [] = xs