2016-02-25 27 views
1

我使用Newtonsoft嘗試和序列化JSON的一些做了HttpWebRequest的POSTJSON序列化錯誤錯誤的請求

我不斷收到一個響應說「錯誤的請求」

我假設我的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(); 
} 
+0

Web服務器可能會返回錯誤的請求的任何數量的原因,不只是格式錯誤的JSON:您可能會發送錯誤的參數或忽略所需的參數;你可能有錯誤的內容類型;您可能會缺少服務器期望的一個或多個標頭;你可能會發送到錯誤的端點;您可能不會將JSON寫入請求流,或者服務器可能有錯誤。 –

回答