C# 序列化的JSON请求结果为;“错误请求”;错误
我正在使用Newtonsoft尝试并序列化一些JSON来发布HttpWebRequest 我不断收到回复说“请求不好” 我假设我的JSON格式不正确。下面是我的代码:C# 序列化的JSON请求结果为;“错误请求”;错误,c#,json.net,httpwebrequest,C#,Json.net,Httpwebrequest,我正在使用Newtonsoft尝试并序列化一些JSON来发布HttpWebRequest 我不断收到回复说“请求不好” 我假设我的JSON格式不正确。下面是我的代码: Account account = new Account(); account.Name = "TESTACCOUNT"; var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://app01.nutshell.com/api/v1/json"); http
Account account = new Account();
account.Name = "TESTACCOUNT";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://app01.nutshell.com/api/v1/json");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = new NetworkCredential("username", "password");
var serializer = new JsonSerializer();
using (var tw = new Newtonsoft.Json.JsonTextWriter(streamWriter))
{
serializer.Serialize(tw,
new
{
method = "newAccount",
@params = account
});
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
如果只是JSON:
{
"method": "newAccount",
"@params": "account"
}
web服务器返回错误请求的原因有很多,不仅仅是JSON格式错误:可能是发送了错误的参数或忽略了所需的参数;您可能有错误的内容类型;您可能缺少服务器期望的一个或多个标头;您可能发送到了错误的端点;您可能没有将JSON写入请求流,或者服务器可能有错误。