PLC总控改造(2)

WebAPI:用于从站向主站推送相关内容

从站编写:

using Newtonsoft.Json;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Text;

public static class WebAPIClientHelper

{

// 主站的IP地址

private static string url = "http://XXXXX:2002/api/push/";

/// <summary>

/// 外部调用:发送 string + 指定类型

/// </summary>

public static bool Send(string message, 指定类型 参数名)

{

try

{

// 组装要发送的数据

var sendObj = new

{

Message = message,

Parameter = parameter

};

// 转JSON

string json = JsonConvert.SerializeObject(sendObj);

byte[] data = Encoding.UTF8.GetBytes(json);

// 使用 .NET4.0 自带的 WebClient(不报错!)

using (WebClient client = new WebClient())

{

client.Headers[HttpRequestHeader.ContentType] = "application/json";
byte[] result = client.UploadData(url, "POST", data);

string resp = Encoding.UTF8.GetString(result);

}

return true;

}

catch

{

return false;

}

}

}

主站编写:

using Newtonsoft.Json;

using System;

using System.Collections.Generic;

using System.IO;

using System.Net;

using System.Text;

using System.Threading;

public class WebAPIServer

{

private HttpListener _listener;

private bool _isRunning;

private Plugin plugin;

public void Start()

{

_listener = new HttpListener();

_listener.Prefixes.Add("http://*:2002/api/push/");//监听

_listener.Start();

_isRunning = true;

System.Threading.ThreadPool.QueueUserWorkItem(o =>

{

while (_isRunning)

{

try

{

var context = _listener.GetContext();

ProcessRequest(context);

}

catch { }

}

});

}

public WebAPIServer(Plugin pluginInstance)

{

plugin = pluginInstance ?? throw new ArgumentNullException(nameof(pluginInstance), "插件实例不能为null");

}

private void ProcessRequest(HttpListenerContext context)

{

try

{

using (var reader = new StreamReader(context.Request.InputStream, Encoding.UTF8))

{

string json = reader.ReadToEnd();

var data = JsonConvert.DeserializeObject<ReceiveData>(json);

string msg = data.Message;

MethodRuningParameter param = data.Parameter;

if (msg == "start")

{

RunStartTask(param);

}

else

{

RunDefault(msg, param);

}

var resp = new { code = 200, msg = "接收成功" };

string respJson = JsonConvert.SerializeObject(resp);

byte[] respBytes = Encoding.UTF8.GetBytes(respJson);

context.Response.ContentType = "application/json";

context.Response.OutputStream.Write(respBytes, 0, respBytes.Length);

}

}

catch

{

context.Response.StatusCode = 500;

}

finally

{

context.Response.Close();

}

}

private void RunStartTask(MethodRuningParameter p)

{

//业务逻辑

}

========================================================================

private void RunDefault(string msg, MethodRuningParameter p)

{

Console.WriteLine($"【默认处理】消息={msg}");

}

public void Stop()

{

_isRunning = false;

_listener?.Stop();

_listener?.Close();

}

}

//==================================================

// 定义接收实体

//==================================================

public class ReceiveData

{

public string Message { get; set; }

public MethodRuningParameter Parameter { get; set; }

}

}

相关推荐
Murphy202321 天前
.net8 Swashbuckle.AspNetCore WEBAPI 配置要点记录
.net·swagger·webapi·swashbuckle
.NET修仙日记21 天前
Acme.ReturnOh:让.NET API返回值处理更优雅,统一响应格式一步到位
c#·.net·webapi
ChaITSimpleLove1 个月前
aiagent-webapi 命令的详细使用说明
dotnet·webapi·ai agent·agent framework·maf·projecttemp
wstcl2 个月前
像asp.net core webapi一样在asp.net frameworks中使用Swagger,方便调试接口
后端·asp.net·swagger·webapi
xiaoid4 个月前
C#向jave平台的API接口推送
c#·post·webapi
csdn_aspnet5 个月前
使用 .NET 8 构建 RESTful Web API
restful·webapi·.net8
csdn_aspnet5 个月前
在 .Net 8 WEBAPI 中实现实体框架的 Code First 方法
webapi·.net8
csdn_aspnet5 个月前
.Net 8 Web API CRUD 操作
webapi·.net8
csdn_aspnet6 个月前
使用 Entity Framework Code First 方法创建 ASP.NET Core 5.0 Web API
.netcore·webapi