我正在尝试使用restsharp和xamarine将一些数据发送到wcf服务器并获取返回值。这是服务器端的代码:
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时,我不断收到“传入的消息有一个意外的消息格式'Raw'。操作的预期消息格式是'Xml','Json'。” 有什么帮助吗?
答案 0 :(得分:3)
您不应该使用AddParamater。这为POST
创建了一个表单编码体代替:
request.RequestFormat = DataFormat.Json;
request.AddBody(new { "username" = "blabla"}));