private void RespCallback(IAsyncResult asynchronousResult) { try { WebRequest myWebRequest1 = (WebRequest)asynchronousResult.AsyncState; // End the Asynchronous response. WebResponse webResponse = myWebRequest1.EndGetResponse(asynchronousResult); } catch (Exception) { // TODO:Log the error } }
Now having the webResponse object, what is the easiest way to read its contents?
GetResponseStream Method (System.Net) Gets the stream that is used to read the body of the response from the server.
I would simply use the async methods on WebClient
- much easier to work with:
WebClient client = new WebClient(); client.DownloadStringCompleted += (sender,args) => { if(!args.Cancelled && args.Error == null) { string result = args.Result; // do something fun... } }; client.DownloadStringAsync(new Uri("http://foo.com/bar"));
But to answer the question; assuming it is text, something like (noting you may need to specify the encoding):
using (var reader = new StreamReader(response.GetResponseStream())) { string result = reader.ReadToEnd(); // do something fun... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With