程序笔记   发布时间:2022-05-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了获取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的请求信息方法实例就是小编分享给大家的全部内容了,希望能给大家一个参,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的获取WebService的请求信息方法实例全部内容,希望文章能够帮你解决获取WebService的请求信息方法实例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

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