獲取WebService的請求信息方法實例
一個已經(jīng)寫好的項目中有多個WebService,由于之前沒有記錄請求信息的,有時候需要查錯等需要找到當次的請求信息,所以需要加入記錄請求信息的功能。
首先想到的是在每一個帶有WebMethod特性的方法里調(diào)用記錄請求信息的方法,這樣可以記錄信息,但是太多帶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將請求信息記錄起來




另一種調(diào)用方式是在另一個項目中添加了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"));
}
}


以上這篇獲取WebService的請求信息方法實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
asp.C#實現(xiàn)圖片文件與base64string編碼解碼
前些天在opera論壇里面當了個flashblocker腳本文件,顧名思義,就是把網(wǎng)頁中的flash給過濾了,過濾之后呢就會在原位置顯示一張圖片,以前用firefox時的flash過濾插件也是這樣,而且顯示的圖片也一樣,一樣的難看,于是就想換換它。2010-03-03
ASP.NET中repeater嵌套實現(xiàn)代碼(附源碼)
repeater嵌套經(jīng)常會在一些特殊效果顯示下會用到,新手朋友們可以詳細看下本文,希望對你有所幫助,代碼很整潔同時附有源碼2013-03-03
Asp.net MVC SignalR來做實時Web聊天實例代碼
本篇文章主要介紹了Asp.net SignalR來做實時Web聊天實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
ASP.NET用戶注冊實戰(zhàn)(第11節(jié))
這篇文章主要介紹了ASP.NET用戶注冊實戰(zhàn),鞏固前10小節(jié)所學的全部知識,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-08-08

