2014-05-13 53 views
0

我試圖使用restsharp和xamarine發送一些數據到服務器WCF並獲得服務器端返回value.Here代碼:WCF客戶restsharp發送RAW格式

public interface IRestService 
{ 
    [OperationContract(Name = "Login")] 
    [WebInvoke(UriTemplate = "/Login/", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json)] 
    Boolean Login(String username); 

和執行登錄的:

Boolean IRestService.Login(string username) 
     { 
      if (string.IsNullOrEmpty(username)) 
       return false; 
      else 
       return true; 
     } 

這裏的是我如何努力讓自己在客戶端連接:

var client = new RestClient("http://192.168.0.187:9226/RestService.svc"); 
      client.AddDefaultHeader("ContentType", "application/json"); 
      var request = new RestRequest(String.Format("/Login/", "198440")); 
      request.Method = Method.POST; 
      request.AddParameter("username", "blabla"); 
      request.RequestFormat = DataFormat.Json; 
      IRestResponse response1 = client.Execute<Boolean>(request); 

當我跟蹤我的WCF,我一直g^etting「傳入的消息具有意外的消息格式'Raw'。該操作的預期消息格式「XML」,「Json的」「 任何幫助

+0

如何將「BodyStyle = WebMessageBodyStyle.Wrapped」更改爲「BodyStyle = webMessageBodyStyle.Bare」 – yyou

回答

3

你不應該使用AddParamater此創建形式編碼體的POST

代替:?

request.RequestFormat = DataFormat.Json; 
request.AddBody(new { "username" = "blabla"}));