最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
获取WebService的请求信息方法实例
时间:2022-06-25 06:55:49 编辑:袖梨 来源:一聚教程网
一个已经写好的项目中有多个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)
{
}
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string QueryBalance(string username,string password)
{
if (username == "test" && password == "abcd")
{
return "1000000";
}
else
{
return "用户名或密码错误";
}
}
这里使用了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"));
}
}
相关文章
- 污污漫画入口风险警示-合规访问指南与安全指引 12-13
- 海棠文学城网页版官网入口-2025在线网址直达 12-13
- 土豪漫画官方APP免费下载入口-正版无广告畅读保障 12-13
- 歪歪漫画app最新版下载入口-首页畅读无广告直接进 12-13
- SkrBT磁力官网最新入口-skrbt磁力引擎极速版入口 12-13
- 微博热搜网页版极速入口-微博热搜榜官方直达链接 12-13





