我正在尝试HttpWebResponse从HttpWebRequest. 我希望得到实际数字(200、301,302、404 等)而不是文字描述。(“Ok”、“MovedPermanently”等)数字是否隐藏在响应对象某处的属性中?除了创建一个大开关功能之外还有什么想法吗?谢谢。
HttpWebResponse
HttpWebRequest
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(的类型response.StatusCode)是一个枚举,其中成员的值与 HTTP 状态代码匹配,例如
response.StatusCode
public enum HttpStatusCode { ... Moved = 301, OK = 200, Redirect = 302, ... }