【问题标题】:How to load webview with post parameters in windows 8?如何在 Windows 8 中加载带有 post 参数的 webview?
【发布时间】:2012-12-12 06:58:35
【问题描述】:

我需要在 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();

【问题讨论】:

    标签: c# windows xaml microsoft-metro


    【解决方案1】:

    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,但这可能有点麻烦。

    【讨论】:

    • 没关系...我正在从响应中获取 htmlstring。但它在没有任何样式或图像的情况下加载到 webview 中。我认为它的 baseurl 问题。但是dony知道如何解决它吗?任何帮助将不胜感激.!!!!!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多