我想获得的HTTP状态code从 HttpWebResponse
对象从返回的数的HttpWebRequest
。我希望得到实际的数字(200,301,302,404等),而不是文本描述。 (好,MovedPermanently等)是响应对象埋在某处财产多少?任何想法不是创建一个大的开关功能等?谢谢。
的HttpWebRequest的WebRequest =(HttpWebRequest的)的WebRequest
.Create(http://www.gooogle.com/);
webRequest.AllowAutoRedirect = FALSE;
HttpWebResponse响应=(HttpWebResponse)webRequest.GetResponse();
//返回MovedPermanently,不是301就是我想要的。
Console.Write(response.Status code.ToString());
解决方案
Console.Write((int)的response.Status code);
的 的HTTPStatus code (类型 response.Status code
)是一个枚举,其中的成员的值相匹配的HTTP状态codeS,如:
公开枚举的HTTPStatus code
{
...
感动= 301,
行= 200,
重定向= 302,
...
}
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.
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());
解决方案
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.
public enum HttpStatusCode
{
...
Moved = 301,
OK = 200,
Redirect = 302,
...
}