C# 从 HttpWebRequest 和 HttpWebResponse 获取 Http 状态代码编号(200、301、404 等)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1330856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Http Status code number (200, 301, 404, etc.) from HttpWebRequest and HttpWebResponse
提问by James Lawruk
I am trying to get the HTTP status code number from the HttpWebResponse
object returned from a HttpWebRequest
. I was hoping to get the actual numbers (200, 301,302, 404, etc.) rather than the text description. ("Ok", "MovedPermanently", etc.) Is the number buried in a property somewhere in the response object? Any ideas other than creating a big switch function? Thanks.
我正在尝试从HttpWebResponse
从HttpWebRequest
. 我希望得到实际数字(200、301,302、404 等)而不是文本描述。(“好的”、“MovedPermanently”等)该数字是否隐藏在响应对象中某处的属性中?除了创建一个大的开关功能之外还有什么想法吗?谢谢。
HttpWebRequest webRequest = (HttpWebRequest)WebRequest
.Create("http://www.gooogle.com/");
webRequest.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
//Returns "MovedPermanently", not 301 which is what I want.
Console.Write(response.StatusCode.ToString());
采纳答案by dtb
Console.Write((int)response.StatusCode);
HttpStatusCode(the type of response.StatusCode
) is an enumeration where the values of the members match the HTTP status codes, e.g.
HttpStatusCode( 的类型response.StatusCode
)是一个枚举,其中成员的值与 HTTP 状态代码匹配,例如
public enum HttpStatusCode
{
...
Moved = 301,
OK = 200,
Redirect = 302,
...
}
回答by zeldi
You have to be careful, server responses in the range of 4xx and 5xx throw a WebException. You need to catch it, and then get status code from a WebException object:
您必须小心,4xx 和 5xx 范围内的服务器响应会抛出 WebException。您需要捕获它,然后从 WebException 对象获取状态代码:
try
{
wResp = (HttpWebResponse)wReq.GetResponse();
wRespStatusCode = wResp.StatusCode;
}
catch (WebException we)
{
wRespStatusCode = ((HttpWebResponse)we.Response).StatusCode;
}
回答by Wojciech Jakubas
As per 'dtb' you need to use HttpStatusCode, but following 'zeldi' you need to be extra careful with code responses >= 400.
根据“dtb”,您需要使用 HttpStatusCode,但在“zeldi”之后,您需要格外小心代码响应 >= 400。
This has worked for me:
这对我有用:
HttpWebResponse response = null;
HttpStatusCode statusCode;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException we)
{
response = (HttpWebResponse)we.Response;
}
statusCode = response.StatusCode;
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
sResponse = reader.ReadToEnd();
Console.WriteLine(sResponse);
Console.WriteLine("Response Code: " + (int)statusCode + " - " + statusCode.ToString());
回答by prestigious
//Response being your httpwebresponse
Dim str_StatusCode as String = CInt(Response.StatusCode)
Console.Writeline(str_StatusCode)
回答by Marc
Just coerce the StatusCode
to int
.
只需强制StatusCode
到int
.
var statusNumber;
try {
response = (HttpWebResponse)request.GetResponse();
// This will have statii from 200 to 30x
statusNumber = (int)response.StatusCode;
}
catch (WebException we) {
// Statii 400 to 50x will be here
statusNumber = (int)we.Response.StatusCode;
}