我使用以下代码来发送大量HTTPRequests并且由于许多调用都不成功,我想检测它们并将它们保存在新列表中(UnsuccessfulURLsPhase1)并再次发送它们。
不幸的是,当HTTPRequest无法发送时,异常不会发生,我无法将url添加到新列表中。
任何提示都表示赞赏。
private static async Task CallURLAsync(string url)
{
try
{
var content = new MemoryStream();
var webReq = (HttpWebRequest)WebRequest.Create(url);
using (WebResponse response = await webReq.GetResponseAsync())
{
}
}
catch ( Exception e )
{
UnsuccessfulURLsPhase1.Add(url);
Console.WriteLine("\nException raised!");
Console.WriteLine("\nMessage:{0}", e.Message);
}
}
答案 0 :(得分:1)
这是代码
var content = new MemoryStream();
var webReq = (HttpWebRequest)WebRequest.Create("http://google.com");
using (WebResponse response = await webReq.GetResponseAsync())
{
HttpWebResponse res = (HttpWebResponse)response;
if(res.StatusCode==HttpStatusCode.OK)
{
//your code
}
}