HttpWebRequest POST给500服务器错误 [英] HttpWebRequest POST gives 500 server error
问题描述
我需要从我的应用程序中获取经过身份验证的httpwebrequest.对我的请求的响应应为json格式.为此,我正在使用以下代码:
I need to make from my app an authentificated httpwebrequest. the response to my request should be in json format. for this i'm using the code below:
// Create the web request
Uri address = new Uri("http://www.mysite.com/remote/user/login/format/json");
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.UseDefaultCredentials = false;
request.Credentials = new NetworkCredential(UserName, Password);
request.PreAuthenticate = true;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/json";
string data = string.Format("username={0}&password={1}", otherusername, otherpassword);
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
// Set the content length in the request headers
request.ContentLength = byteData.Length;
//Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Get response
try
{
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
jsonResponse = reader.ReadToEnd();
reader.Close();
}
user = new User();
JObject o = JObject.Parse(jsonResponse);
user.Unguessable_id = (string)o["unguessable_id"];
user.Print_id = (string)o["print_id"];
user.Rrid = (string)o["rrid"];
user.Raid = (string)o["raid"];
}
catch (WebException ex) {
errorMessage = ex.Message;
}
问题在于,第一次调用它总是在服务器上显示500错误.并且请求失败.如果我重做呼叫(通过在浏览器中刷新),则请求成功.
the problem is that the very first call it always gives a 500 error on the server. and the request fails. if i redo the call(by making an refresh in my browser) the request is successful.
在正常情况下,请求应如下所示:
the request should look like this in normal conditions:
POST /remote/user/login/format/json HTTP/1.1
Host: <yourhost>
username=user&password=pass
但是当服务器发出500错误时,他收到了如下信息:
but when the server sends out the 500 error he received something like this:
username=user&password=passwordPOST /remote/user/login/format/json HTTP/1.1
知道为什么会这样吗?在我的测试应用程序中,如果我刷新使httpwebrequest调用成功的页面.
any idea why this is happening? in my test app if i refresh the page that makes the httpwebrequest the call is successful.
安装Fiddler后,发出的请求如下所示:
after installing Fiddler the requests made look like this:
=>产生500的那个
=> the one that generates 500
POST http://www.mysite.com/remote/user/login/format/json HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Host: www.mysite.com
Content-Length: 30
Expect: 100-continue
Connection: Keep-Alive
username=user&password=pass
=>刷新一次
POST http://www.mysite.com/remote/user/login/format/json HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Authorization: Basic ZGNpOkFpR2g3YWVj
Host: www.mysite.com
Content-Length: 30
Expect: 100-continue
Connection: Keep-Alive
username=user&password=pass
似乎授权:基本ZGNpOkFpR2g3YWVj 未包含在第一个请求中……为什么发生?(我对两个请求都使用相同的代码)
it seems that Authorization: Basic ZGNpOkFpR2g3YWVj is not included in the first request...why is that happening?(i'm using the same code for both requests)
推荐答案
我需要添加:
request.Headers.Add("Authorization: Basic ZGNpOkFpR2g3YWVj");
虽然很奇怪,但第二个请求却是自动添加的.
weird though that for the second request it was added automatically..
这篇关于HttpWebRequest POST给500服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!