我正在使用 WebClient
类将一些数据发布到 Web 表单。我想获取表单提交的响应状态代码。到目前为止,我已经找到了如何在出现异常时获取状态码
Catch wex As WebException
If TypeOf wex.Response Is HttpWebResponse Then
msgbox(DirectCast(wex.Response, HttpWebResponse).StatusCode)
End If
但是,如果表单提交成功并且没有抛出异常,那么我将不知道状态码(200,301,302,...)
有没有办法在没有抛出异常的情况下获取状态码?
PS:我不喜欢使用httpwebrequest/httpwebresponse
最佳答案
您可以检查错误是否为WebException
类型,然后检查响应代码;
if (e.Error.GetType().Name == "WebException")
{
WebException we = (WebException)e.Error;
HttpWebResponse response = (System.Net.HttpWebResponse)we.Response;
if (response.StatusCode==HttpStatusCode.NotFound)
System.Diagnostics.Debug.WriteLine("Not found!");
}
或
try
{
// send request
}
catch (WebException e)
{
// check e.Status as above etc..
}
关于c# - 如何从 webclient 获取状态码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3574659/