一个已经写好的项目中有多个WebService,由于之前没有记录请求信息的,有时候需要查错等需要找到当次的请求信息,所以需要加入记录请求信息的功能。
首先想到的是在每一个带有WebMethod特性的方法里调用记录请求信息的方法,这样可以记录信息,但是太多带WebMethod特性的方法了,于是想在全局中拦截并捕获,于是想到了Global.asax
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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)
{
}
}
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[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的引用,
?
1
2
3
4
5
6
7
8
|
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"));
}
}
|
以上这篇获取WebService的请求信息方法实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/godbell/archive/2017/11/25/7897038.html
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 62
-
springboot+quartz以持久化的方式实现定时任务的代码
2025-05-29 88 -
2025-05-27 70
-
2025-06-04 53
-
淘宝联盟云建站的安全性如何保障?用户信息是否会得到充分保护?
2025-06-04 24
热门评论