C# – How to read a WebClient response after posting data

cnetwebclient

Behold the code:

using (var client = new WebClient())
{
    using (var stream = client.OpenWrite("http://localhost/", "POST"))
    {
        stream.Write(post, 0, post.Length);
    }
}

Now, how do I read the HTTP output?

Best Answer

It looks like you have a byte[] of data to post; in which case I expect you'll find it easier to use:

byte[] response = client.UploadData(address, post);

And if the response is text, something like:

string s = client.Encoding.GetString(response);

(or your choice of Encoding - perhaps Encoding.UTF8)