Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a WebClient response after posting data?

Tags:

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?

like image 283
Jader Dias Avatar asked Jun 18 '09 20:06

Jader Dias


People also ask

How do I get data from WebClient?

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest. RegisterPrefix method. UploadString Sends a String to the resource and returns a String containing any response.

How do you send parameters data using WebClient POST request in C?

var url = "https://your-url.tld/expecting-a-post.aspx" var client = new WebClient(); var method = "POST"; // If your endpoint expects a GET then do it. var parameters = new NameValueCollection(); parameters. Add("parameter1", "Hello world"); parameters. Add("parameter2", "www.stopbyte.com"); parameters.

What is the use of WebClient in C#?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.


2 Answers

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)

like image 190
Marc Gravell Avatar answered Sep 17 '22 15:09

Marc Gravell


If you want to keep streams everywhere and avoid allocating huge arrays of bytes, which is good practise (for example, if you plan to post big files), you still can do it with a derived version of WebClient. Here is a sample code that does it.

using (var client = new WebClientWithResponse()) {     using (var stream = client.OpenWrite(myUrl))     {         // open a huge local file and send it         using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))         {             file.CopyTo(stream);         }     }      // get response as an array of bytes. You'll need some encoding to convert to string, etc.     var bytes = client.Response; } 

And here is the customized WebClient:

public class WebClientWithResponse : WebClient {     // we will store the response here. We could store it elsewhere if needed.     // This presumes the response is not a huge array...     public byte[] Response { get; private set; }      protected override WebResponse GetWebResponse(WebRequest request)     {         var response = base.GetWebResponse(request);         var httpResponse = response as HttpWebResponse;         if (httpResponse != null)         {             using (var stream = httpResponse.GetResponseStream())             {                 using (var ms = new MemoryStream())                 {                     stream.CopyTo(ms);                     Response = ms.ToArray();                 }             }         }         return response;     } } 
like image 38
Simon Mourier Avatar answered Sep 18 '22 15:09

Simon Mourier