当前位置: 移动技术网 > IT编程>开发语言>.net > 获取WebService的请求信息方法实例

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

2017年12月07日  | 移动技术网IT编程  | 我要评论

真锅卡奥丽,东莞螺杆,四季养生之道

一个已经写好的项目中有多个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的请求信息方法实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网