Send HTTP POST Request In Web View Windows UWP, 8.1 Platform

While developing UWP apps, we need to send POST or GET requests to a server for authorization or for downloading files from the web. There is one case when we need web-enabled components while using Post requests. 
 
In this tutorial, we will learn the solution to this problem. 
 
We can use HtttpClient to make our http request with headers and the parameters we need to send to the server. Prepare your url. You should use .net.http namespace for this, not web.http namespace.
  1. string url = "www.yoururl.com/authorize/";  
Now, prepare your post parameters,
  1. string postBody = string.Format("keyvalue=param1&keyvalue2={0}", someValue);  
Make StringContent for this with UTF-8 encoding or whatever your server supports:
  1. StringContent postBodyEncoded = new StringContent(postBody, Encoding.UTF8, "application/x-www-form-urlencoded");  
we use x-www-form-urlencoded to tell that we have parameters in the request body. 
 
Now, declare the HttpClientHandler with AllowRedirect to true. OAuth needs many redirects itself to go from one server to the other. You may switch it off if you don't need it.
  1. HttpClientHandler handler = new HttpClientHandler();  
  2. handler.AllowAutoRedirect = true;  
Create your HttpClient, 
  1. HttpClient client  = new HttpClient(handler);     
 Now, you need to send one post request in webview. Declare a webview in your xaml file.
  1. <WebView x:Name="MyWebView" Source="" />  
We have two options,

Either prepare a HttpRequestMessage with all the params it requires and use:
  1. MyWebView.NavigateWithHttpRequestMessage(httprequest);  
Get the response of the POST request and then navigate the webview to the content of response as usual which is really easy. 
  1. HttpResponseMessage res = await client.PostAsync(url, postBodyContent);  
  2. var content = await res.Content.ReadAsStringAsync();  // read the content from response
  3. MyWebView.NavigateToString(content);                         // now naviagate to the data
Wow, your job is done. You have sent one POST request to server in a web-enabled component (here one webview) succesfully. 
 
What's next: You may have a requirement to get the callback from the webview after your job is done from webview. I shall write a post for this too. Until then, give your feedback on this and tell me if you get some help from it.