Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 序列化的JSON请求结果为;“错误请求”;错误_C#_Json.net_Httpwebrequest - Fatal编程技术网

C# 序列化的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

我正在使用Newtonsoft尝试并序列化一些JSON来发布HttpWebRequest

我不断收到回复说“请求不好”

我假设我的JSON格式不正确。下面是我的代码:

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写入请求流,或者服务器可能有错误。