我有一些执行DownloadStringAsync
的代码。自从我在 IIS 中打开向浏览器发送错误消息后,我可以使用我的浏览器获取有关错误的一些信息。我想知道是否可以在代码中捕获它。
void Process( Job job )
{
using( WebClient client = new WebClient() )
{
client.DownloadStringCompleted += (sender, args ) =>
DownloadStringCompleted(job,args);
try
{
client.DownloadStringAsync(new Uri(job.url), job);
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
}
}
void DownloadStringCompleted(Job job, DownloadStringCompletedEventArgs args)
{
if( args.Error != null )
{
// How can i get the response ? Like 500 Internal Server Error
// this_buggy_page.asp Line 100000
}
}
最佳答案
如果args.Error != null
,它可能会包含WebException
的实例;如果是这种情况,那么您可以将其转换为 WebException
,然后访问其 Response
属性,您可以将其转换为 HttpWebResponse
(对于 HTTP调用),从那里您可以获取标题/正文/等。
关于c# - 使用 System.WebClient 捕获 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6205822/