I’ve got the following code to perform a web request and fetch HTTP response. What I’m trying to do is, IF HTTP Response is
200 OK
I need to read the response body without sending the web request again (I know my code is currently sending it twice).
try
{
using (WebResponse response = request.GetResponse())
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
using (var webClient = new WebClient())
{
string result = webClient.DownloadString(request.RequestUri);
if(result.StartsWith("NUMBER NOT IN LIST"))
{
return "Number Not In List";
}
return result;
}
}
else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
{
return statusCode = HttpStatusCode.Unauthorized.ToString();
}
else if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
{
return statusCode = HttpStatusCode.BadRequest.ToString();
}
}
}
Read the response stream: