gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:46:35 26 4
gpt4 key购买 nike

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

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

在我的应用程序中,我使用了一个 WebView,它可以让用户查看我网站的某些内容。使用 user_id 和 session_id,他自动登录(之后,创建服务器端 session 和 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];

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

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

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

最佳答案

在 Windows 8.1 中使用 WebView 进行 POST

或者更好的是,使用 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 进行 POST

对于 Windows 8,使用 JavaScript 即可!

  1. 创建 <form> , 设置 action到您的目标 URI 并设置 method发布。
  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

关于c# - 在 Windows 应用商店应用程序 WebView 中使用请求发布数据 - 使用 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20646608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com