Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting ordinary Http Post web request with Async and Await

Tags:

How I can convert my traditional HttpWebRequest "POST" call with Async / Await pattern, Here with this I am attaching my current code, Any one please help me to convert this code using Async / Await pattern for windows phone 8.

public void GetEnvironmentVariables(Action<Credentials> getResultCallback, Action<Exception> getErrorCallback) {     CredentialsCallback = getResultCallback;     ErrorCallback = getErrorCallback;     var uri = new Uri(BaseUri);     var request = (HttpWebRequest)WebRequest.Create(uri);     request.Method = "POST";     request.ContentType = "application/json";     var jsonObject = new JObject     {         new JProperty("apiKey",_api),         new JProperty("affiliateId",_affid),     };     var serializedResult = JsonConvert.SerializeObject(jsonObject);     byte[] requestBody = Encoding.UTF8.GetBytes(serializedResult);      request.BeginGetRequestStream(GetRequestStreamCallback, new object[] { request, requestBody });  }  private void GetRequestStreamCallback(IAsyncResult asynchronousResult) {     var request = (HttpWebRequest)((object[])asynchronousResult.AsyncState)[0];     using (var postStream = request.EndGetRequestStream(asynchronousResult))     {         var byteArray = (byte[])((object[])asynchronousResult.AsyncState)[1];          // Write to the request stream.         postStream.Write(byteArray, 0, byteArray.Length);      }     request.BeginGetResponse(GetResponseCallback, request); }  private void GetResponseCallback(IAsyncResult asynchronousResult) {     var request = (HttpWebRequest)asynchronousResult.AsyncState;     try     {         var response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);         if (response != null)         {             var reader = new StreamReader(response.GetResponseStream());             string responseString = reader.ReadToEnd();             Credentails = JsonConvert.DeserializeObject<Credentials>(responseString);             if (Credentails != null && string.IsNullOrEmpty(Credentails.Err))                 CredentialsCallback(Credentails);             else             {                 if (Credentails != null)                     ErrorCallback(new Exception(string.Format("Error Code : {0}", StorageCredentails.Err)));             }         }     }     catch (WebException we)     {             var reader = new StreamReader(we.Response.GetResponseStream());             string responseString = reader.ReadToEnd();             Debug.WriteLine(responseString);             ErrorCallback(we);      } }  
like image 649
Nitha Paul Avatar asked Jan 29 '13 06:01

Nitha Paul


People also ask

What is the benefit of using async and await in a website or web service?

The main benefits of asynchronous programming using async / await include the following: Increase the performance and responsiveness of your application, particularly when you have long-running operations that do not require to block the execution.

Should I use async in web API?

In general, you should make a method asynchronous if the synchronous method blocks the ASP.NET request thread while doing no work. By making the call asynchronous, the ASP.NET request thread is not blocked doing no work while it waits for the web service request to complete.

Should all methods be async?

If a method has no async operations inside it there's no benefit in making it async . You should only have async methods where you have an async operation (I/O, DB, etc.). If your application has a lot of these I/O methods and they spread throughout your code base, that's not a bad thing.

Does async await run on different threads?

Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method. The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread.


1 Answers

Since Windows Phone 8 doesn't seem to offer the TAP methods you need such as GetRequestStreamAsync the first thing to do is write a little wrapper to provide them for yourself:

public static class WebRequestAsyncExtensions {     public static Task<Stream> GetRequestStreamAsync(this WebRequest request)     {         return Task.Factory.FromAsync<Stream>(             request.BeginGetRequestStream, request.EndGetRequestStream, null);     }      public static Task<WebResponse> GetResponseAsync(this WebRequest request)     {         return Task.Factory.FromAsync<WebResponse>(             request.BeginGetResponse, request.EndGetResponse, null);     } } 

Note the use of Task.Factory.FromAsync - this is the preferred way to get an await-friendly wrapper around an APM-based async API such as those offered by WebRequest. This is far more efficient than using Task.Factory.StartNew as suggested by someone else, because that would spin up a new thread, whereas this won't need to.

With this in place, you can now write your code in the same way you would on platforms where these TAP-style methods are available (e.g. Windows 8 store apps, desktop apps, etc.):

public async Task GetEnvironmentVariablesAsync(Action<Credentials> getResultCallback, Action<Exception> getErrorCallback) {     CredentialsCallback = getResultCallback;     ErrorCallback = getErrorCallback;     var uri = new Uri(BaseUri);     var request = (HttpWebRequest) WebRequest.Create(uri);     request.Method = "POST";     request.ContentType = "application/json";     var jsonObject = new JObject     {         new JProperty("apiKey",_api),         new JProperty("affiliateId",_affid),     };     var serializedResult = JsonConvert.SerializeObject(jsonObject);     byte[] requestBody = Encoding.UTF8.GetBytes(serializedResult);      // ASYNC: using awaitable wrapper to get request stream     using (var postStream = await request.GetRequestStreamAsync())     {         // Write to the request stream.         // ASYNC: writing to the POST stream can be slow         await postStream.WriteAsync(requestBody, 0, requestBody.Length);     }      try     {         // ASYNC: using awaitable wrapper to get response         var response = (HttpWebResponse) await request.GetResponseAsync();         if (response != null)         {             var reader = new StreamReader(response.GetResponseStream());             // ASYNC: using StreamReader's async method to read to end, in case             // the stream i slarge.             string responseString = await reader.ReadToEndAsync();             Credentails = JsonConvert.DeserializeObject<Credentials>(responseString);             if (Credentails != null && string.IsNullOrEmpty(Credentails.Err))                 CredentialsCallback(Credentails);             else             {                 if (Credentails != null)                     ErrorCallback(new Exception(string.Format("Error Code : {0}", StorageCredentails.Err)));             }         }     }     catch (WebException we)     {         var reader = new StreamReader(we.Response.GetResponseStream());         string responseString = reader.ReadToEnd();         Debug.WriteLine(responseString);         ErrorCallback(we);      } } 

Note the four lines with // ASYNC: comments - these show where I've made changes. I've collapsed your method down to one, because that's a) possible once you're using async and await and b) much easier than trying to pass things from one method to the next using state arguments.

Notice that the second and fourth of these actually makes async some things you were previously doing synchronously: writing data into the request stream, and reading data out of the response stream. For a small request this probably doesn't matter, but if large amounts of data are being transferred, a synchronous call to Write or ReadToEnd may block. Fortunately, although Windows Phone 8 appears to be missing the TAP methods on WebRequest, it does offer them on Stream and StreamReader so this works without needing to write any extension methods.

like image 185
Ian Griffiths Avatar answered Oct 04 '22 18:10

Ian Griffiths