简体   繁体   中英

In c#( .net) system.net.http, how to get the response.statuscode as an int? Not to use HttpWebRequest and HttpWebResponse

        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();

I want to get the statusCode in integer, how can I do?

HttpResponseMessage.StatusCode是一个HttpStatusCode枚举,它的底层整数类型是int ,所以你可以把它强制转换:

int statusCode = (int)response.StatusCode;

HttpStatusCode is declared as an enum sort of like:

enum HttpStatusCode
{
     NotFound = 404,
     ...
}

Which means you can do it simply by casting:

int status = (int)HttpStatusCode.NotFound;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM