简体   繁体   English

如何通过WCF将JSON HTTP请求发布到RESTful API

[英]How to POST JSON HTTP Request to RESTful API made by WCF

I am trying to make Android consume a simple WCF webservice made by my self. 我正在尝试使Android使用我自己制作的简单WCF Web服务。 Here is my WCF Service definition: 这是我的WCF服务定义:

    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "/UploadCallLog2")]
    [OperationContract]
    String UploadCallLog2(String number, String cached_name, String duration, String date, String line_number);

I have two questions: 1. And what should my HTTP Request Look Like? 我有两个问题:1.我的HTTP请求应该是什么样? What kind of Content type should I choose when add the request header? 添加请求标头时,我应该选择哪种内容类型? 2. Is there the correct way to put input param in body as JSON and the webservice can auto detect the value of input parameters? 2.是否存在将输入参数作为JSON放入正文中的正确方法,并且Web服务可以自动检测输入参数的值?

Fred 弗雷德

I would suggest using an MVC project for this over a WCF service. 我建议通过WCF服务为此使用MVC项目。

The controller can return a JsonResult, the MVC library contains Json helper methods to serialize and deserialize the json objects into c# objects if the properties match. 控制器可以返回JsonResult,MVC库包含Json帮助器方法,以在属性匹配时将json对象序列化和反序列化为c#对象。

Also this then allows the MVC based service to be called from any client that has support for XmlHttpRequest using ContentType as "application/json". 然后,这还将允许使用ContentType作为“ application / json”的任何支持XmlHttpRequest的客户端调用基于MVC的服务。 You can use either HttpGet or HttpPost depending on your request type and security of data under SSL as well. 您还可以使用HttpGet或HttpPost,具体取决于您的请求类型和SSL下的数据安全性。 You can also take advantage of the authentication model to secure your methods. 您还可以利用身份验证模型来保护您的方法。

This just makes for a really clean implementation that doesn't have any complex server and client configurations. 这只是一个真正干净的实现,它没有任何复杂的服务器和客户端配置。 More information regarding similar approaches is the new Web API in MVC 4. 有关类似方法的更多信息是MVC 4中的新Web API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM