C#编写Web设置请求报文都为application/json类型格式

方法一:

创建Web一般处理程序,支持json字符串格式

请求报文例如:

{
"num":"0440200011510190002",
"cls":"stkin",
"ocrTime":"2020-7-23 12:29:30",
"reason":"疯狂调用中"
}

 

代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using YiRan.BLL.WebApi;

namespace YiRan.WebApi
{
    /// <summary>
    /// YiRanWebHandles 的摘要说明
    /// </summary>
    public class YiRanWebHandles : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string sendInfo = string.Empty;
            using (StreamReader reader = new StreamReader(context.Request.InputStream))
            {
                sendInfo = reader.ReadToEnd().Trim();
            }
            if(string.IsNullOrWhiteSpace(sendInfo))
            {
                context.Response.Write("传入字符串不能为空");
                return;
            }
            ActiveStop m = new ActiveStop();
            string res = m.TaskInfo(sendInfo);
            context.Response.Write(res);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

 第二种 WebService

请求报文例如:

{

 "sendInfo":"{'num':'0440200011510190002','cls':'stkin','ocrTime':'2020-7-23 12:29:30','reason':'下错单了'}"

}

using Common;
using Common.DataConversionHelper;
using Common.Log;
using Common.WebHttps;
using Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using YiRan.BLL;
using YiRan.BLL.WebApi;
using YiRan.Entity;
using YiRan.Entity.StandardOrderCancel;

namespace YiRan.WebApi
{
    /// <summary>
    /// YiRanWebMian 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class YiRanWebMian : System.Web.Services.WebService
    {
        [WebMethod(Description = "Erp主动截停单据接口(WMS提供)")]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void SetActiveStop(string sendInfo)
        {
            ActiveStop m = new ActiveStop();
            string res = m.TaskInfo(sendInfo);
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Context.Response.Write(res.ToString());
        }
    }
}

 

posted @ 2022-05-20 09:52  博客YS  阅读(1250)  评论(0)    收藏  举报