我正在尝试从从HttpWebResponse返回的对象中获取HTTP状态代码号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, ... }