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

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了获取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将请求信息记录起来

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

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: