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.
- string url = "www.yoururl.com/authorize/";
- string postBody = string.Format("keyvalue=param1&keyvalue2={0}", someValue);
- StringContent postBodyEncoded = new StringContent(postBody, Encoding.UTF8, "application/x-www-form-urlencoded");
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.
- HttpClientHandler handler = new HttpClientHandler();
- handler.AllowAutoRedirect = true;
- HttpClient client = new HttpClient(handler);
- <WebView x:Name="MyWebView" Source="" />
Either prepare a HttpRequestMessage with all the params it requires and use:
- MyWebView.NavigateWithHttpRequestMessage(httprequest);
- HttpResponseMessage res = await client.PostAsync(url, postBodyContent);
- var content = await res.Content.ReadAsStringAsync(); // read the content from response
- MyWebView.NavigateToString(content); // now naviagate to the data
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.
Israel AguilarPosted Oct 4, 2019, 2:33 AM
NO HAY GRAFICOS DE ESTE TUTORIAL , ME PPUEDES ORIENTAR GRACIAS