【问题标题】:How to fix 400 Bad Request error?如何修复 400 Bad Request 错误?
【发布时间】:2012-04-12 20:37:27
【问题描述】:

我收到一个远程服务器在尝试运行我的代码时返回错误:(400) Bad Request 错误。任何帮助,将不胜感激。谢谢。

    // Open request and set post data
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("myurl.com/restservice/Login");
    request.Method = "POST";
    request.ContentType = "application/json; charset:utf-8";
    string postData = "{ \"username\": \"testname\" },{ \"password\": \"testpass\" }";

    // Write postData to request url
    using (Stream s = request.GetRequestStream())
    {
        using (StreamWriter sw = new StreamWriter(s))
            sw.Write(postData);
    }

    // Get response and read it
    using (Stream s = request.GetResponse().GetResponseStream()) // error happens here
    {
        using (StreamReader sr = new StreamReader(s))
        {
            var jsonData = sr.ReadToEnd();
        }
    }

JSON 编辑

改为:

{ \"username\": \"jeff\", \"password\": \"welcome\" }

但还是不行。

编辑

这是我发现的有效方法:

       // Open request and set post data
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("myurl.com/restservice/Login");
    request.Method = "POST";
    request.ContentType = "application/json";
    string postData = "{ \"username\": \"testname\", \"password\": \"testpass\" }";

    // Set postData to byte type and set content length
    byte[] postBytes = System.Text.UTF8Encoding.UTF8.GetBytes(postData);
    request.ContentLength = postBytes.Length;

    // Write postBytes to request stream
    Stream s = request.GetRequestStream();
    s.Write(postBytes, 0, postBytes.Length);
    s.Close();

    // Get the reponse
    WebResponse response = request.GetResponse();

    // Status for debugging
    string ResponseStatus = (((HttpWebResponse)response).StatusDescription);

    // Get the content from server and read it from the stream
    s = response.GetResponseStream();
    StreamReader reader = new StreamReader(s);
    string responseFromServer = reader.ReadToEnd();

    // Clean up and close
    reader.Close();
    s.Close();
    response.Close();

【问题讨论】:

  • 你为什么要玩 Streams?请改用WebClient
  • WebClient 是个好主意。但还要注意,400 请求通常表示服务器不理解您的请求。错误的有效负载可能是罪魁祸首,尤其是因为您的 JSON 似乎不正确。

标签: c#


【解决方案1】:

你可以试试string postData = "[{ \"username\": \"testname\" },{ \"password\": \"testpass\" }]";

这样你发送一个包含 2 个对象的数组

编辑:也可能您真正想要发送的只是一个具有 2 个属性的对象,那么它将是 string postData = "{ \"username\": \"testname\", \"password\": \"testpass\" }"

【讨论】:

  • 如果数据无效,许多 REST 服务会返回 HTTP 400,例如 Viamenete 和 Palletways
  • 问题可能出在 postData 上吗?看起来它是按字面意思发送 \ 斜线。
  • 我认为这不是问题,它们只是转义字符.. 你在哪里看到那些斜线被发送?
  • 当我将鼠标悬停在 postData 上时处于调试模式。
  • 好的,调试模式会向您显示转义字符串,但您可以确定它们是在没有斜杠的情况下发送的。所以更改 json 字符串不起作用?
【解决方案2】:

看起来它可能来自您发布的 JSON,因为它是无效的,请参阅下面您发送的内容,但格式有效:

{
    "username": "testname",
    "password": "testpass"
}

【讨论】:

  • 正如我在其他评论中所说,如果数据无效,许多 REST 服务会返回 HTTP 400,例如 Viamenete 和 Palletways。我今天刚刚使用了一个基于 HTTP 的 Web 服务,它做同样的事情。
【解决方案3】:

postData 不是有效的 Json 对象

{ "username": "testname" },{ "password": "testpass" }

尝试使用 Json 解析器,例如 Json.NetJavaScriptSerializerDataContractJsonSerializer,而不是手动生成它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-19
    • 2019-09-07
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    相关资源
    最近更新 更多