zoukankan      html  css  js  c++  java
  • WCF系列 Restful WCF

    由于项目需要,需要完成移动端与服务端以json格式的数据交互,所以研究了Restful WCF相关内容,以实现ios端,android端与浏览器端能够与后台服务交互。

    那么首先我们来了解下什么是Restful WCF服务。基于Restful可以将每个url视为一个资源,通过调用url来获取资源数据,通过url中不同的参数来调整获取数据的条件。所以restful形式的服务更加直观,使用也更加简单。

    restful wcf的实现

    [ServiceContract]
    public interface IRESTFulWCF
    {
            [OperationContract]
            string GetList(QueryModel qm);
    }
    
    public class RESTFulWCF : IRESTFulWCF
        {

        /// Method Get,Post,Put,Delete
        /// UriTemplate URI模板,调用方根据模板来构建查询url
        /// RequestFormat/ResponseFormat 请求和响应的数据类型,xml或json
        /// BodyStyle 请求和响应的数据是否包头包尾

            [WebInvoke(Method = "POST", UriTemplate = "GetList", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

    public string GetList(QueryModel qm)
            {
                  return "{'state':'1'}";
            }
    }        

    通过宿主发布该服务后即可在移动端调用,发布地址为:http://localhost:18001/APPData

    这里我列举下ios下调用restful服务的方法,如下:

    NSString *json=@"{"qm":{"pageIndex":0,"rowCount":10000}}";//此为post到服务端的查询条件,json格式
        [HttpHandler HttpPostWithUrlStr:@"http://localhost:18001/APPData/GetList" Paramters:json FinishCallbackBlock:^(NSString *result) {
             NSMutableDictionary *di=[NSMutableDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:[result dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil]];
            if(di)
            {
                NSLog(@"%@",[di objectForKey:@"state"]);
            }
         }];

    按照我们的预期,ios客户端会log到服务端的返回值,这种直接通过url调用服务的方式的确是方便操作。

  • 相关阅读:
    测分用例模板
    功能测试用例设计(24方法)
    pycharm
    Mac-peizhi
    基于Java BufferedImage实现识别图片中的黑色矩形
    Soap从入门到实战
    实列+JVM讲解类的实列化顺序
    基于Springboot的BaseService和BaseController
    Kendo-Grid for Vue API and Template
    web项目jsp中无法引入js问题
  • 原文地址:https://www.cnblogs.com/qyzBlog/p/4206032.html
Copyright © 2011-2022 走看看