【问题标题】:Post data with a request in a Windows Store app WebView - using C#在 Windows 应用商店应用程序 WebView 中发布带有请求的数据 - 使用 C#
【发布时间】:2013-12-17 23:13:25
【问题描述】:

我的应用中有以下场景:

在第一次启动时,用户可以注册一个帐户。然后,该应用程序从我的网络服务器获取一对 (int user_id, string session_id) 并将该数据存储在应用程序中。

在我的应用程序中,我使用 WebView 让用户查看我网站的某些内容。使用 user_id 和 session_id,他会自动登录(之后,创建服务器端会话和 cookie)。

我不想使用像http://mobile.mysite.com/?user_id=int&session_id=string 这样的 url 方案,所以我决定使用 http post 来发送 user_id 和 session_id。

在 iOS 中这很容易:

// Post the user data and load the website in the webview
NSString *startUrl = @"http://mobile.mysite.com/";
NSString *post = [NSString stringWithFormat:@"user_id=%@&session_id=%@, [user uid], [user sessionId]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:startUrl]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[post dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest:request];

所以我在这里,用 C# 在我的 Windows 8 应用商店应用程序中完成了相同的结构。不幸的是,WebView 不允许我将用户信息发布到服务器。

您知道如何解决我的问题吗?

一个想法是发出一个 http 请求并与 WebView 共享 cookie。但这对我来说看起来不太优雅......

【问题讨论】:

    标签: c# post webview windows-store-apps


    【解决方案1】:

    在 Windows 8.1 中使用 WebView 发布

    或者更好的是,使用WebView.NavigateWithHttpRequestMessage(HttpRequestMessage requestMessage)

    您可以使用Windows.Web.Http.HttpRequestMessage 来设置HTTP 方法和请求内容等。

    例如:

    HttpRequestMessage request = new HttpRequestMessage(
        HttpMethod.Post,
        new Uri("http://localhost"));
    request.Content = new HttpStringContent(
        String.Format("user_id={0}&session_id={1}", "Chinese", "food"));
    webView.NavigateWithHttpRequestMessage(request);
    

    这相当于以下 HTTP 请求:

    POST / HTTP/1.1
    Accept: text/html, application/xhtml+xml, */*
    Accept-Language: en-US,en;q=0.5
    User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; MASAJS; WebView/2.0; rv:11.0) like Gecko
    Accept-Encoding: gzip, deflate
    Host: localhost
    Content-Length: 31
    Connection: Keep-Alive
    Cache-Control: no-cache
    
    user_id=Chinese&session_id=food
    

    在 Windows 8 中使用 WebView 发布

    对于 Windows 8,请使用 JavaScript!

    1. 创建一个<form>,将action 设置为您的目标URI,并将method 设置为POST。
    2. 添加两个<input> 并用您想要的名称命名它们,在本例中为user_idsession_id
    3. 添加设置输入值并提交表单的脚本。

    例如:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        webView.NavigateToString(@"<html>
        <head>
            <script type='text/javascript'>
                function doSomething(userIdValue, sessionIdValue) 
                { 
                    document.getElementById('user_id').value = userIdValue;
                    document.getElementById('session_id').value = sessionIdValue;
                    document.getElementById('myForm').submit();
                    return 'Hello World!'; 
                }
            </script>
        </head>
        <body>
            <form id='myForm' action='http://localhost' method='post'>
                <input type='hidden' id='user_id' name='user_id' />
                <input type='hidden' id='session_id' name='session_id' />
            </form>
        </body>
    </html>");
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string result = webView.InvokeScript("doSomething", new string[] { "Chinese", "food" });
    }
    

    这将发送这样的请求:

    POST / HTTP/1.1
    Accept: text/html, application/xhtml+xml, */*
    Accept-Language: en-US,en;q=0.5
    User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch; MASAJS; WebView/1.0)
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate
    Host: localhost
    Content-Length: 31
    Connection: Keep-Alive
    Cache-Control: no-cache
    
    user_id=Chinese&session_id=food
    

    【讨论】:

    • 好的,但如果我想兼容 Windows 8,这不是一个选项,对吧?我觉得排除 Windows 8 用户对我的应用不利?
    • 老实说,@andreas,我认为排除 Windows 8 用户很好,因为 8.1 是免费升级。我敢肯定,仍然在 8 的人数是如此之少。我不认为说“你需要安装免费的 8.1 升级版才能运行这个应用程序”是一件坏事。
    • 我添加了一个 Windows 8 建议。
    • @Kiewic:您应该将内容类型添加到 Windows 8.1 建议中,就像我在 stackoverflow.com/questions/20720856/… 上所做的那样。用户,请注意,由于错误,此方法尚未生效。在这里阅读更多:social.msdn.microsoft.com/Forums/windowsapps/en-US/…
    • 朋友们好,有解决这个问题的办法吗? stackoverflow.com/questions/34224053/…
    【解决方案2】:

    您可以做的是发出 http 请求并将收到的响应作为字符串。一旦你这样做了,你就可以打电话给webView.NavigateToString(responseString)。如果你需要 css,那么你可以随时将 CSS 添加到 html 字符串中。

    我认为这种情况通常是这样处理的。

    更新:

    作为参考的stackoverflow答案:https://stackoverflow.com/a/13862424/329928

    关于 css 的另一个 stackoverflow 答案:https://stackoverflow.com/a/14608917/329928

    【讨论】:

    • 我已经实现了这个想法,但它在两个方面很糟糕:我注意到速度大幅下降。像那样浏览网站并不是很有趣。当我使用 webview 的 goback 方法时,css/js/images 似乎不起作用(正如你提到的)
    • 您使用的是 Win8 还是 Win8.1? Win8 WebView 很可能是我遇到过的最糟糕的事情。听说 8.1 带来了很多改进,但我没有验证。
    • 我在 Win 8.1 上。但我希望 Win 8 用户能够运行我的应用程序。我同意你的意见; WebView 很弱...
    猜你喜欢
    • 2012-12-13
    • 2015-11-22
    • 1970-01-01
    • 2017-03-26
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    相关资源
    最近更新 更多