How to make regular WebRequest asynchronous and expected?

I need to make the following code asynchronous and expected.

I need to get a lot of data from a web server, and then this data will be used to populate the xaml page in my application.

So, I need the DefLogin () method to wait.

Is it possible?

public void DefLogin() { postData = "My Data To Post"; var url = new Uri("Url To Post to", UriKind.Absolute); webRequest = WebRequest.Create(url); webRequest.Method = "POST"; webRequest.ContentType = "text/xml"; webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest); } public void GetRequestStreamCallback(IAsyncResult asynchronousResult) { webRequest = (HttpWebRequest)asynchronousResult.AsyncState; Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); byte[] byteArray = Encoding.UTF8.GetBytes(postData); postStream.Write(byteArray, 0, byteArray.Length); postStream.Close(); Debug.WriteLine("Start BEGINGetResponse"); webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); } public void GetResponseCallback(IAsyncResult asynchronousResult) { try { HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse response; response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult); Stream streamResponse = response.GetResponseStream(); StreamReader streamReader = new StreamReader(streamResponse); string Response = streamReader.ReadToEnd(); streamResponse.Close(); streamReader.Close(); response.Close(); if (Response == "") { //show some error msg to the user Debug.WriteLine("ERROR"); } else { //Your response will be available in "Response" Debug.WriteLine(Response); } } catch (WebException) { //error } } 

I saw this question in StackOverflow: Converting a regular Http Post web request with Async and Await , but I could not correctly understand the answer.

Please, help? I would be very grateful!

+6
source share
1 answer

You can use TaskFactory.FromAsync to convert APM to TAP , creating many tiny extension methods, such as:

 public static Task<Stream> GetRequestStreamAsync(this WebRequest request) { return TaskFactory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null); } 

and do the same for WebRequest.GetResponse and (if necessary) Stream.Write , Stream.Flush , etc.

Then you can write your actual logic using async and await without callbacks:

 public async Task DefLoginAsync() { postData = "My Data To Post"; var url = new Uri("Url To Post to", UriKind.Absolute); webRequest = WebRequest.Create(url); webRequest.Method = "POST"; webRequest.ContentType = "text/xml"; using (Stream postStream = await webRequest.GetRequestStreamAsync()) { byte[] byteArray = Encoding.UTF8.GetBytes(postData); await postStream.WriteAsync(byteArray, 0, byteArray.Length); await postStream.FlushAsync(); } try { string Response; using (var response = (HttpWebResponse)await webRequest.GetResponseAsync()); using (Stream streamResponse = response.GetResponseStream()) using (StreamReader streamReader = new StreamReader(streamResponse)) { Response = await streamReader.ReadToEndAsync(); } if (Response == "") { //show some error msg to the user Debug.WriteLine("ERROR"); } else { //Your response will be available in "Response" Debug.WriteLine(Response); } } catch (WebException) { //error } } 
+13
source

Source: https://habr.com/ru/post/951625/


All Articles