这是用户@Kiewic建议使用网络视图发布帖子请求的后续问题:Post data with a request in a Windows Store app WebView - using C#
所以我正在使用此代码
// defined before: string uri, User user
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(uri));
request.Content = new HttpStringContent(
String.Format(
"language={0}&session_id={1}&user_id={2}",
Text.GetLanguage(),
user.session_id,
user.user_id.ToString()
),
Windows.Storage.Streams.UnicodeEncoding.Utf8,
"application/x-www-form-urlencoded"
);
webView.NavigateWithHttpRequestMessage(request); // webView defined in xaml
Fiddler向我展示了这个要求:
POST http://mobile.mysite.com/ HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US,en;q=0.8,de-CH;q=0.5,de;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; SMJB; WebView/2.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: mobile.mysyte.com
Content-Length: 101
Connection: Keep-Alive
Pragma: no-cache
Cookie: _ga=GA1.2.769009833.1387593479; PHPSESSID=nk6b04rb7d7vu4vmm2ish7l0o4
language=en&session_id=fhihferihguiegierewfrefghxrfer&user_id=1
网络服务器技术是PHP,因此我使用此代码打印帖子内容
<?php
var_dump($_POST);
?>
但结果是一个空数组。那我的错误在哪里?
答案 0 :(得分:0)
根据Microsoft员工的说法,设置请求内容的标头存在错误。这应该意味着我的代码在第一篇文章中没有任何问题: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/7deaf4ba-611f-4957-af3c-9b2b2e1e1b8b/webviewnavigatewithhttprequestmessage-set-contenttype?forum=winappswithcsharp
这意味着您无法真正使用NavigateWithHttpRequest,您应该使用其他方法发布数据。用户@Kiewic提出了一个适用于Windows 8的建议: Post data with a request in a Windows Store app WebView - using C#