【问题标题】:WCF client restsharp sending raw formatWCF客户端restsharp发送原始格式
【发布时间】:2014-05-13 13:58:51
【问题描述】:

我正在尝试使用 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'。” 有什么帮助吗?

【问题讨论】:

  • 如何将“BodyStyle = WebMessageBodyStyle.Wrapped”改为“BodyStyle = webMessageBodyStyle.Bare”

标签: wcf xamarin restsharp


【解决方案1】:

您不应该使用 AddParamater。这将为 POST 创建一个表单编码主体

改为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2011-05-04
    • 2023-03-09
    • 2023-04-06
    • 2019-11-06
    相关资源
    最近更新 更多