分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

获取WebService的请求信息

发布时间:2023-09-06 01:27责任编辑:顾先生关键词:Web

一个已经写好的项目中有多个WebService,由于之前没有记录请求信息的,有时候需要查错等需要找到当次的请求信息,所以需要加入记录请求信息的功能。

首先想到的是在每一个带有WebMethod特性的方法里调用记录请求信息的方法,这样可以记录信息,但是太多带WebMethod特性的方法了,于是想在全局中拦截并捕获,于是想到了Global.asax

public class Global : System.Web.HttpApplication ???{ ???????protected void Application_Start(object sender, EventArgs e) ???????{ ???????} ???????protected void Session_Start(object sender, EventArgs e) ???????{ ???????} ???????protected void Application_BeginRequest(object sender, EventArgs e) ???????{ ???????????if (Request != null) ???????????{ ???????????????try ???????????????{ ???????????????????if (".asmx".Equals(Request.CurrentExecutionFilePathExtension,StringComparison.OrdinalIgnoreCase) && Request.ContentLength > 0) ???????????????????{ ???????????????????????using (MemoryStream ms = new MemoryStream()) ???????????????????????{ ???????????????????????????Request.InputStream.CopyTo(ms); ???????????????????????????ms.Position = 0; ???????????????????????????using (StreamReader reader = new StreamReader(ms)) ???????????????????????????{ ???????????????????????????????LogHelper.Info(reader.ReadToEnd()); ???????????????????????????} ???????????????????????} ???????????????????????????????????????????} ??????????????????????????????????} ???????????????catch (Exception) ???????????????{ ???????????????} ???????????????finally ???????????????{ ???????????????????Request.InputStream.Position = 0; ???????????????} ???????????} ???????} ???????protected void Application_AuthenticateRequest(object sender, EventArgs e) ???????{ ???????} ???????protected void Application_Error(object sender, EventArgs e) ???????{ ???????} ???????protected void Session_End(object sender, EventArgs e) ???????{ ???????} ???????protected void Application_End(object sender, EventArgs e) ???????{ ???????} ???}
View Code
[WebMethod]public string HelloWorld(){ ???return "Hello World";}[WebMethod]public string QueryBalance(string username,string password){ ???if (username == "test" && password == "abcd") ???{ ???????return "1000000"; ???} ???else ???{ ???????return "用户名或密码错误"; ???}}
View Code

这里使用了Log4Net将请求信息记录起来

另一种调用方式是在另一个项目中添加了WerService的引用,

public partial class WebForm1 : System.Web.UI.Page ???{ ???????protected void Page_Load(object sender, EventArgs e) ???????{ ???????????TestWebServiceSoapClient client = new TestWebServiceSoapClient(); ???????????Response.Write(client.QueryBalance("test","abcd")); ???????} ???}
View Code

获取WebService的请求信息

原文地址:http://www.cnblogs.com/godbell/p/7897038.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved