我正在使用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!");
}
or
try
{
// send request
}
catch (WebException e)
{
// check e.Status as above etc..
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)