0

我需要在 Metro 应用程序中使用 webview 加载网站。我可以使用以下方法发布登录参数。如何在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();
4

1 回答 1

3

WebView 类不支持直接导航到带有 POST 参数的 URL。

但是,您可以使用您的代码和 WebResponse 来获取 HTML。然后使用 WebView 类的 NavigateToString 方法来渲染 HTML:

            HttpWebResponse httpResponse= (HttpWebResponse)response;
            StreamReader reader=new StreamReader(httpResponse.GetResponseStream());
            string htmlString= reader.ReadToEnd();
            if (!string.IsNullOrEmpty(htmlString))
                webView.NavigateToString(htmlString);

或者,您可以使用带有您的帖子值和一些 javascript 的表单创建自己的 HTML 来提交表单,但这可能会更麻烦一些。

于 2012-12-13T14:54:19.430 回答