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调用服务的方式的确是方便操作。

  • 相关阅读:
    NLP(十六):Faiss应用
    推荐系统(一):DeepFm原理与实战
    NLP(十五):word2vec+ESIM进行文本相似度计算
    NLP(十四):Transformer—用BERT,RoBERTa,XLNet,XLM和DistilBERT文本分类
    1.22学习总结:流计算概述
    1.21学习总结:将RDD转换成DataFrame
    1.20学习总结:DataFrame保存及常用操作
    1.19学习总结:SparkSQL
    1.18学习总结:Spark向HBase写入数据
    1.17学习总结:编写程序读取HBase数据
  • 原文地址:https://www.cnblogs.com/qyzBlog/p/4206032.html
Copyright © 2011-2022 走看看