C#/ASP.NET自定义restful接口,接收三方请求

2022/9/11 1:23:13

本文主要是介绍C#/ASP.NET自定义restful接口,接收三方请求,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

背景

笔者最近在做非标自动化的标准化工作,其中一项工作就是要求设备厂商按照MES厂商(我司)的要求,开放一个接口接收MES的派工任务。

厂商只要实现这个接口,我们就可以通过java调用厂商的服务了,于是有了这篇文章,文章只是抛砖引玉,实现方式供参考。

代码实现

1、Visual Stduio新建asp.net项目,新建一个ashx文件,代码如下

sendOrder.ashx

using System.IO;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using AST_AUTO.Models;

namespace AST_AUTO
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class OrderReceiveHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var request = context.Request;
            var response = context.Response;

            var contentType = request.ContentType;
            var httpMethod = request.HttpMethod;
            if (httpMethod == "POST" && contentType.StartsWith("application/json"))
            {

                response.ContentType = "application/json; charset=UTF-8";
                Stream bodyStream = context.Request.InputStream;
                string requestPayload = string.Empty;
                bodyStream.Position = 0;

                using (var streamReader = new StreamReader(bodyStream, Encoding.UTF8))
                {
                    //读取Body的时候,请尽量使用异步方式读取。ASP.NET Core默认是不支持同步读取的,会抛出异常
                    //解决方法 启用  KestrelServerOptions 中 AllowSynchronousIO 
                    requestPayload = streamReader.ReadToEnd();
                    bodyStream.Position = 0;
                }
                //反序列化
                ReceiveOrder order = (ReceiveOrder)JsonConvert.DeserializeObject(requestPayload,typeof(ReceiveOrder));

                Models.ComResult<string> result = new Models.ComResult<string>();

                result.Success = false;
                result.Data = "0x123 read image buffer error!";
                result.Code = "PLC_ERROR_01";
                result.Message = "PLC设备XXX未就绪";
                string json = JsonConvert.SerializeObject(result, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
                response.Write(context.Server.UrlDecode(json));
            }
        }

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

ComResult.cs如下

namespace AST_AUTO.Models
{
    public class ComResult<T>
    {
        private bool _success;
        private string _message;
        private string _code;
        private T _data;

        public bool Success { get => _success; set => _success = value; }
        public string Message { get => _message; set => _message = value; }
        public string Code { get => _code; set => _code = value; }
        public T Data { get => _data; set => _data = value; }
    }
}

package.json

<?xml version="1.0" encoding="utf-8"?>
<packages>
 <!--其他省略-->
  <package id="Newtonsoft.Json" version="12.0.2" targetFramework="net45" />
</packages>

测试

 

 

 

 

输入参数:

{
    "dispatchNo": "1000021-10010",
    "dispatchQuantity": 100
}

输出参数:

{
    "success": false,
    "message": "PLC设备XXX未就绪",
    "code": "PLC_ERROR_01",
    "data": "0x123 read image buffer error!"
}

 



这篇关于C#/ASP.NET自定义restful接口,接收三方请求的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程