HttpWebRequest POST给出了500个服务器错误

时间:2010-10-19 10:38:57

标签: c# httpwebrequest

我需要从我的应用程序中制作一个经过身份验证的httpwebrequest。我的请求的响应应该是json格式。为此,我使用下面的代码:

// 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错误。并且请求失败。如果我重做呼叫(通过在我的浏览器中刷新)请求成功。

在正常情况下,请求应如下所示:

POST /remote/user/login/format/json HTTP/1.1
Host: <yourhost>

username=user&password=pass

但是当服务器发出500错误时,他收到了这样的信息:

username=user&password=passwordPOST /remote/user/login/format/json HTTP/1.1

知道为什么会这样吗?在我的测试应用程序中,如果我刷新使httpwebrequest成功调用的页面。

修改 在安装Fiddler之后,请求看起来像这样:

=&GT;产生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

=&GT;在刷新时做的那个

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 未包含在第一个请求中...为什么会发生这种情况?(我对两个请求都使用相同的代码)

2 个答案:

答案 0 :(得分:2)

我建议您安装Fiddler以查看实际发生的事情

答案 1 :(得分:2)

我需要添加:

request.Headers.Add("Authorization: Basic ZGNpOkFpR2g3YWVj");

虽然对于第二个请求它是自动添加的但是很奇怪..