var hcHandler = new HttpClientHandler();
//hcHandler.AllowAutoRedirect = false;
var hc = new HttpClient(hcHandler);
hc.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
String url = "http://passport.cnblogs.com/login.aspx";
var task = hc.GetAsync(new Uri(url));
HttpResponseMessage response = task.Result;
string statusCode = response.StatusCode.ToString();
我想以整数形式获取statusCode,我该怎么办?
答案 0 :(得分:3)
HttpResponseMessage.StatusCode
是一个HttpStatusCode
枚举,其基础整数类型为int
,因此您只需将其转换为:
int statusCode = (int)response.StatusCode;
答案 1 :(得分:3)
HttpStatusCode
被声明为枚举类似:
enum HttpStatusCode
{
NotFound = 404,
...
}
这意味着你可以通过施放来实现:
int status = (int)HttpStatusCode.NotFound;