Ye Myat Aung
Ye Myat Aung

Reputation: 1853

How to read HTTP Response Body?

I need to read the HTTP Response 200 OK with the following text/plain body. (Either one)

OK

NUMBER NOT IN LIST

ERROR

But I only know how to read the HTTP Response but not the response body. Explanation with example would be much appreciated

Upvotes: 0

Views: 4621

Answers (2)

Tarun
Tarun

Reputation: 2958

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://example.com/path/to/file");

        request.UseDefaultCredentials = true;

        if (request.Proxy == null)
        {
            request.Proxy = new WebProxy("http://example.com");
        }
        request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; 

HttpWebResponse response = (HttpWebResponse)(request.GetResponse());

Upvotes: 0

dtb
dtb

Reputation: 217391

You can use the WebClient.DownloadString Method to make an HTTP GET request and get the HTTP response body as string returned:

using (var client = new WebClient())
{
    string result = client.DownloadString("http://example.com/path/to/file");

    switch (result)
    {
        case "OK":
        case "NUMBER NOT IN LIST":
        case "ERROR":
            break;
    }
}

Upvotes: 3

Related Questions