【发布时间】:2014-03-04 09:15:42
【问题描述】:
有时简单的事情会让你难过,这就是我的一个。
我想做一个简单的网络请求来验证用户名和密码。它在 Windows Phone 8 中工作得很好,但我似乎无法在 Windows 8 上获得相同的代码。
我知道我不能像使用 Windows Phone 那样执行 GetResponse,所以我使用 GetResponseAsync 以及如果工作正常的那部分。但是来自服务器的响应是它没有在标头中获取“POST”组件。
这是在我的手机版本上运行良好的 Windows Phone 8 代码
private async void VerifyUser()
{
//System.Diagnostics.Debug.WriteLine("aa");
loginParams = "username=" + username + "&password=" + password;
string teamResponse = "https://mysite.com/mystuff/LoginApp?" + loginParams;
var request = HttpWebRequest.Create(teamResponse) as HttpWebRequest;
request.Method = "POST";
request.Accept = "application/json;odata=verbose";
var factory = new TaskFactory();
var task = factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
//System.Diagnostics.Debug.WriteLine("bb");
try
{
var response = await task;
System.IO.Stream responseStream = response.GetResponseStream();
string data;
using (var reader = new System.IO.StreamReader(responseStream))
{
data = reader.ReadToEnd();
}
responseStream.Close();
//System.Diagnostics.Debug.WriteLine("cc");
webData = data;
//MessageBox.Show(data);
}
catch (Exception e)
{
MessageBox.Show("There was a network error. Please check your network connectivty and try again " + e);
}
// System.Diagnostics.Debug.WriteLine(webData);
JToken token = JObject.Parse(webData);
string success = (string)token.SelectToken("success");
这是我使用 Visual Studio 2013 为 Windows 8 提供的内容
private async void VerifyUser()
{
string data;
loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
string teamResponse = "https://mysite.com/mystuff/LoginApp?" + loginParams;
Debug.WriteLine(teamResponse);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(teamResponse);
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
using (var sr = new StreamReader(response.GetResponseStream()))
{
data = sr.ReadToEnd();
}
Debug.WriteLine(data);
}
这可行,但我得到一个简单的响应,仅告知用户是否已登录。服务器小伙说请求的标头中没有“POST”。
所以我添加了以下代码:
request.Method = "POST";
request.Accept = "application/json;odata=verbose";
这是完整的代码:
private async void VerifyUser()
{
string data;
loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
string teamResponse = "https://mysite.com/mystuff/LoginApp?" + loginParams;
Debug.WriteLine(teamResponse);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(teamResponse);
request.Method = "POST";
request.Accept = "application/json;odata=verbose";
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
using (var sr = new StreamReader(response.GetResponseStream()))
{
data = sr.ReadToEnd();
}
Debug.WriteLine(data);
}
然后它只是抛出以下内容:
'web1.exe' (CLR v4.0.30319: Immersive Application Domain): Loaded 'C:\Windows\system32\WinMetadata\Windows.Foundation.winmd'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.Net.WebException' occurred in mscorlib.dll
An exception of type 'System.Net.WebException' occurred in mscorlib.dll but was not handled in user code
Additional information: The remote server returned an error: (411) Length Required.
那么为什么我不能像 doco 所说的那样在标题中包含“POST”?任何帮助将不胜感激。
更新:我现在知道它是 loginParams 长度的问题。在我的 IOS、Android 和 WindowsPhone 应用程序中,我不必指定长度并且效果很好,但 Visual Studio 2013 Windows 应用程序由于某种原因不接受设置内容长度。
这是错误: System.Net.HttpWebRequest 不包含 ContentLength 的定义,并且没有扩展方法 ContentLength 接受 System.Net.HttpWebRequest 类型的第一个参数(您是否缺少 using 指令或程序集引用?)
那为什么我不能添加 request.ContentLength???
在响应 Domniks 对代码的请求时,我附上了带有错误消息的代码块图像。再次感谢您的帮助。 我已经附上了我的屏幕图像,它不会acc
【问题讨论】:
标签: c# xaml visual-studio-2013 windows-8.1