获取WebService的请求信息方法实例

网络编程 发布日期:2024/10/15 浏览次数:1

正在浏览:获取WebService的请求信息方法实例

一个已经写好的项目中有多个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将请求信息记录起来

获取WebService的请求信息方法实例

获取WebService的请求信息方法实例

获取WebService的请求信息方法实例

获取WebService的请求信息方法实例

另一种调用方式是在另一个项目中添加了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的请求信息方法实例

获取WebService的请求信息方法实例

以上这篇获取WebService的请求信息方法实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?