如何在Windows 8中使用发布参数加载Webview? [英] How to load webview with post parameters in windows 8?
问题描述
我需要在Metro应用中使用Webview加载网站.我可以使用以下方法发布登录参数.如何在webview中加载相同的内容??????
I need to load website using webview in metro apps. I could post login parameters using following method. how to load the same in webview??????
string post_data = "userName=test123&password=test@321";
// this is where we will send it
string uri = "http://tesproject.com";
// create a request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
// turn our request string into a byte stream
byte[] postBytes = Encoding.UTF8.GetBytes(post_data);
// this is important - make sure you specify type this way
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = await request.GetRequestStreamAsync();
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
// grab te response and print it out to the console along with the status code
WebResponse response = await request.GetResponseAsync();
推荐答案
WebView类不支持直接使用POST参数导航到URL.
WebView class doesn't support navigating to an URL with POST parameters directly.
但是,您可以使用代码和WebResponse来获取HTML.然后使用WebView类的NavigateToString方法呈现HTML:
You could however use your code and the WebResponse to get the HTML. And then use the NavigateToString method of the WebView class to render the HTML:
HttpWebResponse httpResponse= (HttpWebResponse)response;
StreamReader reader=new StreamReader(httpResponse.GetResponseStream());
string htmlString= reader.ReadToEnd();
if (!string.IsNullOrEmpty(htmlString))
webView.NavigateToString(htmlString);
或者您可以使用带有您的帖子值的表单和一些JavaScript来创建自己的HTML,以提交表单,但这可能会比较麻烦.
Or you could create your own HTML with a form with your post values and some javascript to submit the form, but that might be a bit more cumbersome.
这篇关于如何在Windows 8中使用发布参数加载Webview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!