通过HttpModule管道,帮助api对接开发

  我们公司的技术以.net为主,最近公司的项目需要和其它以java为主的公司搞对接。

    .net提供webapi由java请求调用。

  目前出现java说调用了,但是.net一直接收不到数据。两方开发人员沟通能力都不足,导致问题一直无法解决。

  现不知道是.net接收不到信息的问题,还是model和传过来的参数不匹配导致无法解析的问题。

  为了验证是否接收到信息。我通过module管道拦截请求的数据

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using SFast;

namespace DBModels
{
    public class MyHttpModule :IHttpModule
    {
        public void Dispose() { }

        #region IHttpModule 成员
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(Application_EndRequest);
        }

        #endregion

        public void Application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;

            HttpContext context = application.Context;
            StreamReader reader = new StreamReader(context.Request.InputStream);
            string data = reader.ReadToEnd();
            StreamWriter sw = new StreamWriter("c:\\error.txt", true);
            try
            {
                sw.WriteLine("\r\n");
                sw.WriteLine(DateTime.Now.ToString() + "    QueryString :" + context.Request.QueryString.ToString());
                sw.WriteLine(DateTime.Now.ToString() + "    Headers :" + context.Request.Headers.ToString());
                sw.WriteLine(DateTime.Now.ToString() + "    参数body :" + data);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                sw.Flush();
                sw.Close();
            }
        }
    }
}

通过以上代码,发现参数和.net创建的module匹配不上,导致webapi解析不了。

 注意:通过以上代码获取参数后,就可以删除了。操作中我发现context.Request.InputStream 被读取后就无数据了。代码页面就接收不到数据了。

 

posted on 2016-03-18 11:13  sjns  阅读(349)  评论(0编辑  收藏  举报

导航