【问题标题】:HttpWebRequest POST gives 500 server errorHttpWebRequest POST 给出 500 服务器错误
【发布时间】:2010-10-19 10:38:56
【问题描述】:

我需要从我的应用中创建一个经过身份验证的 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 后发出的请求如下所示:

=> 产生 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

似乎 Authorization: Basic ZGNpOkFpR2g3YWVj 未包含在第一个请求中...为什么会发生这种情况?(我对两个请求使用相同的代码)

【问题讨论】:

    标签: c# httpwebrequest


    【解决方案1】:

    我建议您安装 Fiddler 以查看实际情况

    【讨论】:

      【解决方案2】:

      我需要补充:

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

      奇怪的是,对于第二个请求,它是自动添加的..

      【讨论】:

      • 我想我已经回答了你的问题:msdn.microsoft.com/en-us/library/…。尝试使用示例中的 CredentialCache。据我了解,问题是客户端不知道如何进行身份验证,因为在第一次请求时,服务器还没有告诉。但是,在第一个响应中很清楚:这是基本身份验证。使用 CredentialCache,您可以指定要使用的身份验证方案并避免第一次请求时出错。这不是错误,而是 HTTP 协议的限制。
      • Hy Onkelborg,我按照文章中的建议修改了我的代码,但我收到了来自服务器的 500 错误。如果我只是像我一样添加标题,它会起作用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多