request.Method = "POST";工作但不是 request.ContentLength

编程入门 行业动态 更新时间:2024-10-28 15:27:42
本文介绍了request.Method = "POST";工作但不是 request.ContentLength的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有时简单的事情会让你难堪,这对我来说是一件.

Some times the simple things can stump you and here is one for me.

我想做一个简单的网络请求来验证用户名和密码.它在 Windows Phone 8 中运行良好,但我似乎无法获得相同的代码以在 Windows 8 上运行.

I want to do a simple web request to verify a username and password. Its working just fine in Windows Phone 8 but I can not seem to get the same code to work on Windows 8.

我知道我不能像使用 Windows Phone 那样执行 GetResponse,所以我使用 GetResponseAsync 并且如果工作正常,那部分.但是来自服务器的响应是它没有在标头中获得POST"组件.

I understand I can not do a GetResponse as I do with Windows Phone so I am using GetResponseAsync and that part if working fine. But the response from the Server is that it did not get the "POST" component in the header.

这是在我的手机版本上运行良好的 Windows Phone 8 代码

Here is the Windows Phone 8 code that is working fine on my Phone version

private async void VerifyUser() { //System.Diagnostics.Debug.WriteLine("aa"); loginParams = "username=" + username + "&password=" + password; string teamResponse = "mysite/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 提供的内容

Here is what I have for Windows 8 using Visual Studio 2013

private async void VerifyUser() { string data; loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString; string teamResponse = "mysite/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".

That works but I get back a simple response advising only that the user is logged in or not logged in. The Server chap says that the request did not have "POST" in the header.

所以我添加了以下代码:

SO I added the following code:

request.Method = "POST"; request.Accept = "application/json;odata=verbose";

这里是完整的代码:

private async void VerifyUser() { string data; loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString; string teamResponse = "mysite/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); }

然后它只是抛出以下内容:

And then it just throws the following:

'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"?任何帮助将不胜感激.

So why can I not include the "POST" in the header as the doco says I can? Any help would be much appreciated.

更新:我现在知道这是 loginParams 长度的问题.在我的 IOS、Android 和 WindowsPhone 应用程序中,我不必指定长度并且效果很好,但是由于某些原因,Visual Studio 2013 Windows 应用程序不接受设置内容长度.

UPDATE: I now know its an issue with the length of the loginParams. In my IOS, Android and WindowsPhone apps I did not have to specify the length and it works great, but the Visual Studio 2013 Windows App does not accept setting the content length for some reason.

错误如下:System.Net.HttpWebRequest 不包含 ContentLength 的定义,并且找不到接受 System.Net.HttpWebRequest 类型的第一个参数的扩展方法 ContentLength(您是否缺少 using 指令或程序集引用?)

那为什么我不能添加 request.ContentLength???

So why can I not add request.ContentLength???

在对 Domniks 代码请求的响应中,我附上了带有错误消息的代码块图像.再次感谢您的帮助.我附上了我的屏幕图像,它不会被接收

IN Response to Domniks request for the code I have attached a image of the block of code with the error message. Thanks again for helping. I have attached an image of my screen where it wont acc

推荐答案

已解决!!!

使用 Visual Studio 2013 执行 Http Web 请求和响应的正确过程是使用新的 HttpClient 类.我怀疑它会是这样的新课程,但找不到它.浪费了我生命中的 2 天!!

The correct process to do Http web requests and response with Visual Studio 2013 is to use the new HttpClient class. I suspected it would be some new class like this but just could not find it. 2 days of my life have been wasted!!

所以这里是执行 Http 请求的正确代码,例如登录或在我的情况下仅根据用户 ID 和密码验证用户是有效用户.

So here is the correct code to do a Http request to for example log in or in my case to just verify the user is a valid user based on the userID and password.

private async void VerifyUser() { loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString; string teamResponse = " mySite?" + loginParams; Debug.WriteLine(teamResponse); HttpClient client = new HttpClient(); try { HttpResponseMessage response = await client.PostAsync(new Uri(teamResponse), null); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); Debug.WriteLine(responseBody); } catch (HttpRequestException e) { Debug.WriteLine("\nException Caught!"); Debug.WriteLine("Message :{0} ", e.Message); }

感谢 dbugger 和 Dominik 的帮助,因为您的评论使我朝着让我找到这个解决方案的方向前进.

And thanks heaps to dbugger and Dominik for your help as your comments moved me in the direction that got me to this solution.

更多推荐

request.Method = "POST";工作但不是 request.ContentLength

本文发布于:2023-05-26 16:37:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/259567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!