C# 如何修复400错误请求错误?

C# 如何修复400错误请求错误?,c#,C#,我收到一个错误消息,远程服务器在尝试运行我的代码时返回了一个错误:(400)错误请求错误。任何帮助都将不胜感激。谢谢 // Open request and set post data HttpWebRequest request = (HttpWebRequest)WebRequest.Create("myurl.com/restservice/Login"); request.Method = "POST"; request.ContentType = "ap

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

    // 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();

你能试试
string postData=“[{\'username\':\'testname\'},{\'password\':\'testpass\'}]”吗

这样,您将发送一个包含2个对象的数组


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

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

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

postData不是有效的Json对象

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

尝试使用类似Json的解析器,或者不手动生成它

为什么要处理流?用一个新的来代替!WebClient是个好主意。但是还要注意,400请求通常表示服务器不理解您的请求。错误的负载可能是罪魁祸首,特别是因为您的JSON似乎不正确。如果数据无效,许多REST服务会返回HTTP 400,例如Viamenete和Palletways。问题可能是postData吗?它看起来像是在按字面意思发送\斜杠。我不认为这会是一个问题,它们只是转义字符。。您在哪里看到这些斜杠被发送?当我将鼠标悬停在postData上时,处于调试模式。好的,调试模式将显示转义字符串,但您可以确保它们在发送时没有斜杠。所以更改json字符串是行不通的?正如我在其他评论中所说,如果数据无效,许多REST服务都会返回HTTP 400,例如Viamenete和Palletways。我今天刚刚使用了一个基于HTTP的web服务,它可以做完全相同的事情。