接球System.WebClient 500内部服务器错误 [英] Catch 500 internal server error with System.WebClient
问题描述
我有一些代码 DownloadStringAsync
。我布劳尔我能得到有关该错误的一些信息,因为我打开发送错误信息到浏览器中的IIS。我在想,如果有可能赶上它的代码。
I have some code that does DownloadStringAsync
. with my brower i can get some information about the error since I turned on send error message to browser in IIS. I was wondering if it's possible to catch it in the code.
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
,然后访问它的响应
属性,你可以投到 HttpWebResponse
(用于HTTP调用),并从那里你可以得到头/身/等。
If args.Error != null
it will likely contain an instance of WebException
; if this is the case, then you can cast it to WebException
, then access its Response
property, which you can cast to HttpWebResponse
(for HTTP calls), and from there you can get the headers / body / etc.
这篇关于接球System.WebClient 500内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!