Kyle Russell
Kyle Russell

Reputation: 69

How to Check for HttpStatusCode.OK in GetResponse

I was just wondering how to check if the HTTP status for okay before returning a value.

Here's the code snippet

var httpResponse = await httpWebRequest.GetResponseAsync();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();
        dynamic data = JObject.Parse(responseText);
            shorturl = data.shortLink;
    }
return shorturl;

Was thinking of something along the lines of

if responseText.Contains(HttpStatusCode.OK)

Or

if (httpResponse.StatusCode == HttpStatusCode.OK)
return shorturl;

Anything to point me in the right direction would be appreciated!

Upvotes: 1

Views: 1655

Answers (1)

abhinavsinghvirsen
abhinavsinghvirsen

Reputation: 2034

Internally HttpStatusCode is enum

public enum HttpStatusCode
{
    ...
    Moved = 301,
    OK = 200,
    Redirect = 302,
    ...
}

Then

HttpStatusCode statusCode;

//your response code

statusCode = response.StatusCode;


if ((int)statusCode == HttpStatusCode.OK)
return shorturl;

Upvotes: 2

Related Questions