(原創)面向對象的系統對接接口編寫。第5篇(完結) (原創)多系統間須要對接,我寫了一個接口框架。實用性很是強,寫出來你們交流。須要的能夠直接搬過去用。(第1篇) (原創)面向對象的系統對接接口編寫。

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

本篇是完結篇。主要講如何開始調用了,以及如何控制必須是Get請求或者必須是POST請求,是怎麼限定住的。

圖片

如上圖,咱們以新聞模塊爲例子,建立一個News.ashx的前端處理文件


圖片

圖片

圖片

html

 
<%@ 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=addappnewsjson

 

若是是取新聞資料,那麼他的Get請求的url是這樣子:http://www.163.com/news.ashx?action=getappnewsbyuserid&userid=1app

 

 

當你願意看到這裏時,後續還有4篇:下面是連接:框架

(原創)多系統間須要對接,我寫了一個接口框架。實用性很是強,寫出來你們交流。須要的能夠直接搬過去用。(第1篇) http://www.cnblogs.com/mazhiyuan/p/5224046.htmlpost

(原創)面向對象的系統對接接口編寫。第2篇 http://www.cnblogs.com/mazhiyuan/p/5224049.htmlthis

(原創)面向對象的系統對接接口編寫。第3篇 http://www.cnblogs.com/mazhiyuan/p/5224050.htmlurl

(原創)面向對象的系統對接接口編寫。第4篇 http://www.cnblogs.com/mazhiyuan/p/5224054.htmlspa

(原創)面向對象的系統對接接口編寫。第5篇(完結) http://www.cnblogs.com/mazhiyuan/p/5224056.html日誌

 

若是看完,有不明白的能夠評論發給我。

 

真的很好用的。。有須要作接口的同窗。。能夠把整個框架拿去用下。

 

提供源碼下載,請點擊:源碼

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息