• webservice支持httpget及post方法


    1.在web.config中节点system.web添加

        <webServices>
          <protocols>
            <add name="HttpGet" />
            <add name="HttpPost" />
          </protocols>
        </webServices>

    2.在调用类添加特性

    [System.Web.Script.Services.ScriptService]

    3.在调用方法添加特性

    [WebMethod,ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]

    我这边测试ScriptMethod特性可不加,并且暂时无法指定方法只能使用POST模式,不知哪位指导下。

    调用的话,示例:http://localhost:26813/WebService/xxxx.asmx/GetTest?inputStr=1

    并且在config中添加HttpGet及HttpPost方式访问后,会使全部接口都变成可以使用http方法调用,若有影响请慎用。

    解决返回json字符串时多出一个{“d”:null}字符串方案:

    可以参考连接去看看吧

    下面纯属复制:

    添加文件MyModule:

     1     public class MyModule : IHttpModule
     2     {
     3         public void Dispose()
     4         {
     5         }
     6 
     7         public void Init(HttpApplication context)
     8         {
     9             context.BeginRequest += new EventHandler(Application_BeginRequest);
    10         }
    11 
    12         public void Application_BeginRequest(object sender, EventArgs e)
    13         {
    14             HttpApplication application = sender as HttpApplication;
    15             string extension = Path.GetExtension(application.Request.Path);
    16             //这边我进行一个筛选
    17             if (application.Request.Path.IndexOf(".asmx/SendPatientDischargeInfo") > -1)
    18             {
    19                 application.Response.Filter = new CatchTextStream(application.Response.Filter);
    20             }
    21         }
    22     }
    23 
    24     public class CatchTextStream : Stream
    25     {
    26         #region 数据流
    27         /// <summary>
    28         /// 数据流
    29         /// </summary>
    30         private readonly Stream output;
    31         #endregion
    32         #region 构造函数
    33         public CatchTextStream(Stream s)
    34         {
    35             output = s;
    36         }
    37         #endregion
    38         #region 重载属性及方法
    39         public override bool CanRead => output.CanRead;
    40 
    41         public override bool CanSeek => output.CanSeek;
    42 
    43         public override bool CanWrite => output.CanWrite;
    44 
    45         public override void Flush()
    46         {
    47             output.Flush();
    48         }
    49         public override long Length => output.Length;
    50 
    51         public override long Position
    52         {
    53             get => output.Position;
    54             set => output.Position = value;
    55         }
    56         public override int Read(byte[] buffer, int offset, int count)
    57         {
    58             return output.Read(buffer, offset, count);
    59         }
    60         public override long Seek(long offset, SeekOrigin origin)
    61         {
    62             return output.Seek(offset, origin);
    63         }
    64         public override void SetLength(long value)
    65         {
    66             output.SetLength(value);
    67         }
    68         public override void Write(byte[] buffer, int offset, int count)
    69         {
    70             if (HttpContext.Current != null)
    71             {
    72                 HttpContext context = HttpContext.Current;
    73                 Encoding encoding = context.Response.ContentEncoding;
    74                 string responseInfo = encoding.GetString(buffer, offset, count);
    75                 responseInfo = responseInfo.Replace("{"d":null}", "");
    76                 //responseInfo = responseInfo.Substring(0, responseInfo.Length - 9);
    77                 buffer = encoding.GetBytes(responseInfo);
    78                 output.Write(buffer, 0, buffer.Length);
    79             }
    80         }
    81         #endregion
    82     }
    View Code

    并在web.config中system.webServer节点下的modules节点添加:

    <add name="MyModule" type="命名空间.MyModule"/>

    在Write方法中替换字符串。

  • 相关阅读:
    jquery.md5
    LoginPasswordHelp
    RSA(非对称加密算法、公钥加密算法)
    Swiper 3.4.1
    layer web 弹窗
    操作系统
    查看命令帮助
    软件卸载
    重定向命令
    终端命令格式的组成
  • 原文地址:https://www.cnblogs.com/FlyStupidBird/p/13431621.html
Copyright © 2020-2023  润新知