sc 1 year ago
parent
commit
2c1d2c0e5f
  1. 4
      MainWindow.xaml.cs
  2. 120
      UserClass/HttpSERVER.cs
  3. 4
      formula_manage.csproj
  4. 1
      packages.config

4
MainWindow.xaml.cs

@ -75,6 +75,8 @@ namespace formula_manage
InitializeComponent();
this.Closing += Window_Closing; //添加窗口关闭事件
UserClass.HttpSERVER.Start(); //开启网路通讯
USER.Text = App.USER_Purview;
RRODUCTdataTable.Columns.Add("ID", typeof(int));
RRODUCTdataTable.Columns.Add("DYELOT", typeof(string));
@ -263,6 +265,8 @@ namespace formula_manage
System.IO.DirectoryInfo log = new System.IO.DirectoryInfo(@logpath);//生成日志文件目录
FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write);
StreamWriter wr = new StreamWriter(fs);//创建文件
wr.WriteLine(Log_time + "HttpSERVER_STOP");
UserClass.HttpSERVER.Stop(); //停止网络通信
wr.WriteLine(Log_time + "FORMULA_STOP");
wr.Close();
}

120
UserClass/HttpSERVER.cs

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Net;
namespace formula_manage.UserClass
{
public class HttpSERVER
{
private static bool isExcute = true;
private static HttpListener listener = new HttpListener();
public static void Start()
{
System.Threading.ThreadPool.QueueUserWorkItem(w => Excute());//单独开启一个线程执行监听消息
}
private static void Excute()
{
if (HttpListener.IsSupported)
{
if (!listener.IsListening)
{
listener.Prefixes.Add("http://127.0.0.1:8888/"); //添加需要监听的url
listener.Start(); //开始监听端口,接收客户端请求
}
while (isExcute)
{
try
{
//阻塞主函数至接收到一个客户端请求为止 等待请求
HttpListenerContext context = listener.GetContext();
//解析请求
HttpListenerRequest request = context.Request;
//构造响应
HttpListenerResponse response = context.Response;
//http请求方式:get,post等等
string httpMethod = request.HttpMethod?.ToLower();
string rawUrl = request.RawUrl;//不包括IP和端口
var Url = request.Url;//全路径
if (httpMethod == "get")
{
//获取查询参数
var queryString = request.QueryString;
// 请求接口 test/method?id=5
//键值对方式 string val = queryString["key"];
//string val = queryString["id"];val的值是5
}
else if (httpMethod == "post")
{
//请求接口 test/postMethod 格式是json
//{
// "id":5,
// "name":"zs"
//}
//获取pots请求的请求体,json格式的字符串
var reader = new StreamReader(request.InputStream);
var questBody = reader.ReadToEnd();
if (!string.IsNullOrEmpty(rawUrl))
{
//这里可以直接用switch这个只是demo
if (rawUrl.Equals("/server/uploadgenconnected", StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrEmpty(questBody))
{
UploadGenConnectedModel model = JsonConvert.DeserializeObject<UploadGenConnectedModel>(questBody);
if (model != null)
{
// To Do
}
}
}
}
}
var responseString = string.Empty;
// 执行其他业务逻辑
//*****************
responseString = JsonConvert.SerializeObject(new { code = 1, msg = "发送成功" });
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
//对客户端输出相应信息.
response.ContentLength64 = buffer.Length;
//response.StatusCode = 200;
//response.ContentType = "text/plain";
//发送响应
using (System.IO.Stream output = response.OutputStream)
{
output.Write(buffer, 0, buffer.Length);
}
}
catch (Exception exceotion)
{
//Logger.Error("处理消息异常", exceotion);
string str = exceotion.Message;
}
}
}
else
{
//Logger.Info("系统不支持HttpListener");
}
}
public static void Stop()
{
isExcute = false;
if (listener.IsListening)
listener.Stop();
}
}
public class UploadGenConnectedModel
{
public bool GenConnected { get; set; }
}
}

4
formula_manage.csproj

@ -164,6 +164,9 @@
<Reference Include="ModernUI.Xceed.Toolkit, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\SamOatesGames.ModernUI.Xceed.Toolkit.1.0.1905191936\lib\net45\ModernUI.Xceed.Toolkit.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NModbus, Version=3.0.81.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\NModbus.3.0.81\lib\net46\NModbus.dll</HintPath>
</Reference>
@ -263,6 +266,7 @@
<Compile Include="UserClass\CRCcheck16.cs" />
<Compile Include="UserClass\DataGridHelper.cs" />
<Compile Include="UserClass\HardwareSN.cs" />
<Compile Include="UserClass\HttpSERVER.cs" />
<Compile Include="UserClass\IniFile.cs" />
<Compile Include="UserClass\Log.cs" />
<Compile Include="UserClass\MD5check.cs" />

1
packages.config

@ -11,6 +11,7 @@
<package id="Microsoft.CSharp" version="4.7.0" targetFramework="net462" />
<package id="ModernUI.WPF" version="1.0.9" targetFramework="net46" />
<package id="MvvmLightLibs" version="5.4.1.1" targetFramework="net46" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net472" />
<package id="NModbus" version="3.0.81" targetFramework="net462" />
<package id="SamOatesGames.ModernUI.Xceed.Toolkit" version="1.0.1905191936" targetFramework="net46" />
<package id="Smart.FormDesigner" version="1.4.3" targetFramework="net462" />

Loading…
Cancel
Save