惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
V
Visual Studio Blog
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
L
LangChain Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
P
Proofpoint News Feed
博客园_首页
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 愚

Revit:二开使用Sqlite保存本地数据,并配合EF6等ORM框架 Revity:查找并修改类型参数和实例参数 复习一下UML Revit:ElementFilter过滤器基类 Revit:Element、Reference、ReferenceIntersector、ReferenceWithContext的理解 Revit:使用代码如何发出选点、选对象指令 Reivt:过滤器FilteredElementIterator,采集器FilteredElementCollector Revit:Transaction事务 Revit:弄清楚Application、UIApplication、UIDocument 、Document、DocumentSet之间的关系 Revit:IExternalCommand和IExternalApplication的区别 什么是假程序员 若我有一天 聊聊面试官那些事。。分享我的个人看法,认同的顶一顶。。。 提供一个中国身份证号码判断的类 分享一个自己写的取中国农历相关数据的类。包含:农历年月日,生肖,星座,年龄,天干,地支等方法 从结亲网 的架构谈起,谈什么架构, 我理解的架构。我想很多人理解的架构应该可能比较 狭义 面向对象的全套“企业微信”api接口的代码实现,网上太多“面向过程”微信api接口的代码,这个开源给需要的人用 解决:百度编辑器UEditor,怎么将图片保存到图片服务器,或者上传到ftp服务器的问题(如果你正在用UE,这篇文章值得你看下) (原创)面向对象的系统对接接口编写。第4篇
(原创)面向对象的系统对接接口编写。第5篇(完结)
· 2016-02-28 · via 博客园 - 愚

接上一篇:http://www.cnblogs.com/mazhiyuan/p/5224054.html

本篇是完结篇。主要讲如何开始调用了,以及如何控制必须是Get请求或者必须是POST请求,是怎么限定住的。

图片

如上图,我们以新闻模块为例子,创建一个News.ashx的前端处理文件

图片

图片

图片

<%@ WebHandler Language="C#" Class="News" %>
using System;
using System.Web;
using System.IO;
using System.Web.Script.Serialization;
using ZGMZ.Model;
using ZGMZ.BLL;
using ZGMZ.UIL.App;
using ZGMZ.UIL.App.News;
using System.Collections.Specialized;
using ZGMZ.UIL.Log;
using ZGMZ.Config;
/// <summary>
/// 接受POST方式传输
/// </summary>
public class News : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string action = context.Request["action"];
        string httpMethod = context.Request.HttpMethod.ToLower();
        if (string.IsNullOrEmpty(action))
        {
            context.Response.Write("{\"CodeId\":\"" + Code.FailActionIsNull.CodeId + "\",\"CodeDescription\":\"" + Code.FailActionIsNull.Description + "\",\"ErrorMessage\":\"请传递action参数\"}");
            return;
        }
        CommandType type;
        if (!Enum.TryParse(action, out type))
        {
            context.Response.Write("{\"CodeId\":\"" + Code.FailActionIsNotExists.CodeId + "\",\"CodeDescription\":\"" + Code.FailActionIsNotExists.Description + "\",\"ErrorMessage\":\"传递的Action值未在系统中注册\"}");
            return;
        }
        BaseCommand command = Factory.AppFactory().GetNewsFacade().CreateCommandInstance(type);
        if (httpMethod == "post")
        {
            Post post = command as Post;
            if (post == null)
            {
                context.Response.Write("{\"CodeId\":\"" + Code.FailHttpMethodError.CodeId + "\",\"CodeDescription\":\"" + Code.FailHttpMethodError.Description + "\",\"ErrorMessage\":\"建议尝试更换请求方式\"}");
                return;
            }
            post.Input = this.GetQueryParameters(context);
            try
            {
                post.Excute();
            }
            catch (System.Exception exp)
            {
                context.Response.Write("{\"CodeId\":\"" + Code.FailServer.CodeId + "\",\"CodeDescription\":\"" + Code.FailServer.Description + "\",\"ErrorMessage\":\"" + exp.Message + "\"}");
                return;
            }
        }
        if (httpMethod == "get")
        {
            Get get = command as Get;
            if (get == null)
            {
                context.Response.Write("{\"CodeId\":\"" + Code.FailHttpMethodError.CodeId + "\",\"CodeDescription\":\"" + Code.FailHttpMethodError.Description + "\",\"ErrorMessage\":\"建议尝试更换请求方式\"}");
                return;
            }
            get.Input = context.Request.Params;
            try
            {
                get.Excute();
            }
            catch (System.Exception exp)
            {
                context.Response.Write("{\"CodeId\":\"" + Code.FailServer.CodeId + "\",\"CodeDescription\":\"" + Code.FailServer.Description + "\",\"ErrorMessage\":\"" + exp.Message + "\"}");
                return;
            }
        }
        //LogAbstract log = LogFactory.GetFileLog();
        //log.FileLogServerPath = Params.Global.FileLogServerPath;
        //log.WriteLog("app日志", command.Output);
        context.Response.Write(command.Output);
    }
    /// <summary>
    /// 取传输过来的参数
    /// </summary>
    /// <author>马志远</author>
    private string GetQueryParameters(HttpContext context)
    {
        Stream jsonDataStream = context.Request.InputStream;
        StreamReader reader = new StreamReader(jsonDataStream);
        string jsonData = reader.ReadToEnd();
        reader.Close();
        return jsonData;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

代码细说:
string action = context.Request["action"];    请求的类型。比如要添加新闻,那么action的值就是之前讲的AddAppNews。必须是这个值,以便于反射。名字必须跟AddAppNews.cs的一样。

string httpMethod = context.Request.HttpMethod.ToLower();    获取请求方式。这个是用来控制为什么请求必须是Get或者必须是POST的原因。

if (!Enum.TryParse(action, out type))    当action名字跟.cs文件不一致时,就会报错。

Post post = command as Post;    限制了请求必须是POST,如果他是GET发过来的,那么这个值post就会为null,然后返回错误消息给调用调。

Get get = command as Get;    同样的,这个代码,限制了请求必须是Get。如果用了POST方式,那么这个get对象就会为null.

get.Excute();    接着就开始处理相关业务了。

context.Response.Write(command.Output);    最后,使用command.Output将序列化成json或者xml格式的字符串,返回给调用方。

 最后:以我们添加新闻的为例子。。 他的POST请求的URL应该是这样子:http://www.163.com/News.ashx?action=addappnews

如果是取新闻资料,那么他的Get请求的url是这样子:http://www.163.com/news.ashx?action=getappnewsbyuserid&userid=1

当你愿意看到这里时,后续还有4篇:下面是链接:

(原创)多系统间需要对接,我写了一个接口框架。实用性非常强,写出来大家交流。需要的可以直接搬过去用。(第1篇) http://www.cnblogs.com/mazhiyuan/p/5224046.html

(原创)面向对象的系统对接接口编写。第2篇 http://www.cnblogs.com/mazhiyuan/p/5224049.html

(原创)面向对象的系统对接接口编写。第3篇 http://www.cnblogs.com/mazhiyuan/p/5224050.html

(原创)面向对象的系统对接接口编写。第4篇 http://www.cnblogs.com/mazhiyuan/p/5224054.html

(原创)面向对象的系统对接接口编写。第5篇(完结) http://www.cnblogs.com/mazhiyuan/p/5224056.html

如果看完,有不明白的可以评论发给我。

真的很好用的。。有需要做接口的同学。。可以把整个框架拿去用下。

提供源码下载,请点击:源码