亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

獲取WebService的請求信息方法實(shí)例

 更新時(shí)間:2017年11月26日 08:49:34   作者:Jichan·Jong  
下面小編就為大家分享一篇獲取WebService的請求信息方法實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

一個(gè)已經(jīng)寫好的項(xiàng)目中有多個(gè)WebService,由于之前沒有記錄請求信息的,有時(shí)候需要查錯(cuò)等需要找到當(dāng)次的請求信息,所以需要加入記錄請求信息的功能。

首先想到的是在每一個(gè)帶有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 "用戶名或密碼錯(cuò)誤";
 }
}

這里使用了Log4Net將請求信息記錄起來

另一種調(diào)用方式是在另一個(gè)項(xiàng)目中添加了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的請求信息方法實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論