4
        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?

1

2 Answers 2

3

HttpResponseMessage.StatusCode is a HttpStatusCode enum, whose underlying integer type is int, so you can just cast it:

int statusCode = (int)response.StatusCode;
Sign up to request clarification or add additional context in comments.

Comments

3

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.