我正在尝试从从HttpWebResponse
返回的对象中获取HTTP状态代码号HttpWebRequest
。我希望获得实际数字(200、301、302、404等),而不是文字说明。(“ Ok”,“ 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());
对于相反的操作:httpResponse.HTTPStatusCode =(HttpStatusCode)Enum.ToObject(typeof(HttpStatusCode),404))
—
Leandro