您的位置:首页 > 其它

(原创)面向对象的系统对接接口编写。第5篇(完结)

2016-02-28 08:58 369 查看
接上一篇:/article/5241850.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篇) /article/5241847.html

(原创)面向对象的系统对接接口编写。第2篇 /article/5241848.html

(原创)面向对象的系统对接接口编写。第3篇 /article/5241849.html

(原创)面向对象的系统对接接口编写。第4篇 /article/5241850.html

(原创)面向对象的系统对接接口编写。第5篇(完结) /article/5241851.html

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

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

提供源码下载,请点击:源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: