C# request.Method=";邮政「;正在工作但未请求。ContentLength
有些时候,简单的事情会让你难堪,这是给我的 我想做一个简单的web请求来验证用户名和密码。它在WindowsPhone8中工作正常,但我似乎无法在Windows8上获得相同的代码 我知道我不能像使用Windows Phone那样使用GetResponse,所以我使用GetResponseAsync,如果工作正常的话。但是服务器的响应是,它没有在头中获取“POST”组件 这是Windows Phone 8代码,在我的手机版本上运行良好C# request.Method=";邮政「;正在工作但未请求。ContentLength,c#,xaml,visual-studio-2013,windows-8.1,C#,Xaml,Visual Studio 2013,Windows 8.1,有些时候,简单的事情会让你难堪,这是给我的 我想做一个简单的web请求来验证用户名和密码。它在WindowsPhone8中工作正常,但我似乎无法在Windows8上获得相同的代码 我知道我不能像使用Windows Phone那样使用GetResponse,所以我使用GetResponseAsync,如果工作正常的话。但是服务器的响应是,它没有在头中获取“POST”组件 这是Windows Phone 8代码,在我的手机版本上运行良好 private async void VerifyUser(
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");
这是可行的,但我得到了一个简单的回复,建议用户是否登录。服务器chap表示请求头中没有“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.
byte[] bytes = Encoding.UTF8.GetBytes(loginParams);
request.ContentLength = bytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
那么,为什么我不能像doco说的那样将“POST”包含在标题中呢?任何帮助都将不胜感激
更新:我现在知道这是一个关于登录图长度的问题。在我的IOS、Android和WindowsPhone应用程序中,我不必指定长度,而且效果很好,但Visual Studio 2013 Windows应用程序由于某些原因不接受设置内容长度
以下是错误:
System.Net.HttpWebRequest不包含ContentLength的定义,并且找不到接受System.Net.HttpWebRequest类型的第一个参数的扩展方法ContentLength(是否缺少using指令或程序集引用?
那么为什么我不能添加request.ContentLength
为了响应Domniks对代码的请求,我附上了带有错误消息的代码块的图像。再次感谢你的帮助。
我在屏幕上贴了一张图片,它不会显示你的信息,因为你的所有数据都在URL中。您可以尝试更改获取的方法,因为这就是您正在做的。或者,您可以将post数据写入请求对象的请求流,并真正进行post。请参阅快速示例。由于您的所有数据都在URL中,因此您并不是真正发布信息。您可以尝试更改获取的方法,因为这就是您正在做的。或者,您可以将post数据写入请求对象的请求流,并真正进行post。请参阅快速示例。由于您的所有数据都在URL中,因此您并不是真正发布信息。您可以尝试更改获取的方法,因为这就是您正在做的。或者,您可以将post数据写入请求对象的请求流,并真正进行post。请参阅快速示例。由于您的所有数据都在URL中,因此您并不是真正发布信息。您可以尝试更改获取的方法,因为这就是您正在做的。或者,您可以将post数据写入请求对象的请求流,并真正进行post。请参阅快速示例。获取参数的URL编码与您的URL类似:
“https://mysite.com/mystuff/LoginApp?“+LoginParms
这意味着您总是发送GET参数,只是更改dopost方法不会更改任何内容。
如果要发送POST参数,请执行以下操作:
'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.
byte[] bytes = Encoding.UTF8.GetBytes(loginParams);
request.ContentLength = bytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
GET参数的URL编码与URL类似:
“https://mysite.com/mystuff/LoginApp?“+LoginParms
这意味着您总是发送GET参数,只是更改dopost方法不会更改任何内容。
如果要发送POST参数,请执行以下操作:
'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.
byte[] bytes = Encoding.UTF8.GetBytes(loginParams);
request.ContentLength = bytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
GET参数的URL编码与URL类似:
“https://mysite.com/mystuff/LoginApp?“+LoginParms
这意味着您总是发送GET参数,只是更改dopost方法不会更改任何内容。
如果要发送POST参数,请执行以下操作:
'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.
byte[] bytes = Encoding.UTF8.GetBytes(loginParams);
request.ContentLength = bytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
GET参数的URL编码与URL类似:
“https://mysite.com/mystuff/LoginApp?“+LoginParms
这意味着您总是发送GET参数,只是更改dopost方法不会更改任何内容。
如果要发送POST参数,请执行以下操作:
'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.
byte[] bytes = Encoding.UTF8.GetBytes(loginParams);
request.ContentLength = bytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
解决了
使用Visual Studio 2013执行Http web请求和响应的正确过程是使用新类。我怀疑会有这样的新课程,但就是找不到。我生命中的两天被浪费了
下面是执行Http请求的正确代码,例如登录,或者在我的情况下,根据用户ID和密码验证用户是否有效
private async void VerifyUser()
{
loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
string teamResponse = "https:// mySite.com?" + 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的帮助,因为您的评论将我推向了解决方案的方向。解决了
使用Visual Studio 2013执行Http web请求和响应的正确过程是使用新类。我怀疑会有这样的新课程,但就是找不到。我生命中的两天被浪费了
下面是执行Http请求的正确代码,例如登录,或者在我的情况下,根据用户ID和密码验证用户是否有效
private async void VerifyUser()
{
loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
string teamResponse = "https:// mySite.com?" + 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的帮助,因为您的评论将我推向了解决方案的方向。解决了
使用Visual Studio 2013执行Http web请求和响应的正确过程是使用新类。我怀疑会有这样的新课程,但就是找不到。我生命中的两天被浪费了
下面是执行Http请求的正确代码,例如登录,或者在我的情况下,根据用户ID和密码验证用户是否有效
private async void VerifyUser()
{
loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
string teamResponse = "https:// mySite.com?" + 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的帮助,因为您的评论将我推向了解决方案的方向。解决了
使用Visual Studio 2013执行Http web请求和响应的正确过程是使用新类。我怀疑会有这样的新课程,但就是找不到。我生命中的两天被浪费了
这是正确的代码t