在Windows应用商店应用WebView中使用请求发布数据 - 使用C#

 别装了gg_925 发布于 2023-02-09 18:03

我的应用程序中有以下场景:

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

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

我不想使用像http://mobile.mysite.com/?user_id=int&session_id=string这样的网址方案,所以我决定使用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.但这对我来说并不是很优雅......

1 个回答
  • 在Windows 8.1中使用WebView进行POST

    甚至更好,使用WebView.NavigateWithHttpRequestMessage(HttpRequestMessage requestMessage).

    您可以使用a 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进行POST

    对于Windows 8,使用JavaScript进行操作!

      创建一个<form>,设置action目标URI并设置method为POST.

      添加两个<input>并将它们命名为你想要的名称,在这种情况下user_idsession_id.

      添加一个设置输入值并提交表单的脚本.

    例如:

    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
    

    2023-02-09 18:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有