2013-04-23 160 views
0

我正在嘗試將一些json發佈到jboss服務。使用restSharp ..我的代碼如下。服務器無法識別json,使用restSharp發送請求

 RestClient client = new RestClient(baseURL); 
     RestRequest authenticationrequest = new RestRequest(); 
     authenticationrequest.RequestFormat = DataFormat.Json; 
     authenticationrequest.Method = Method.POST; 
     authenticationrequest.AddParameter("text/json",     authenticationrequest.JsonSerializer.Serialize(prequestObj), ParameterType.RequestBody); 

,也試過這一個

 RestClient client = new RestClient(baseURL); 
     RestRequest authenticationrequest = new RestRequest(); 
     authenticationrequest.RequestFormat = DataFormat.Json; 
     authenticationrequest.Method = Method.POST;       authenticationrequest.AddBody(authenticationrequest.JsonSerializer.Serialize(prequestObj)); 

但在這兩種情況下,我的服務器是給我的錯誤JSON是格式不正確

回答

0

我發現了什麼事情錯了...

我在窗口Metro風格使用RestSharp,所以下載的源代碼,並進行一些修改...所以,在功能修改PutPostInternalAsync我只是說這修改

httpContent = new StringContent(Parameters[0].Value.ToString(), Encoding.UTF8, "application/json"); 

和它解決問題....

參數[0] .Value.ToString(),而這可以寫其可以返回序列化JSON對象的方法。 (如字符串)。

0

嘗試使用JsonHelper準備你的JSON作爲以下

​​

然後

authenticationrequest.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody); 
+0

沒有成功,當我檢查服務器日誌時,它沒有得到體內的json字符串。 – 2013-04-23 11:22:16

+0

它是否給你同樣的錯誤? – user2309699 2013-04-23 11:26:41

+0

是同樣的錯誤... – 2013-04-23 11:44:05