浏览代码

接口上传修改

callm 2 月之前
父节点
当前提交
b86f18cf20
共有 1 个文件被更改,包括 47 次插入4 次删除
  1. 47 4
      UAS_MES_HYSX/PublicMethod/HttpServer.cs

+ 47 - 4
UAS_MES_HYSX/PublicMethod/HttpServer.cs

@@ -2,17 +2,18 @@
 using System.Collections.Generic;
 using System.Data;
 using System.Dynamic;
-using System.Linq;
+using System.Net.Sockets;
 using System.Security.Cryptography;
 using System.Text;
+using HslCommunication;
+using System.Threading;
 using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
 using UAS_MES_NEW.DataOperate;
+using System.IO;
 
 namespace UAS_MES_NEW.PublicMethod
 {
 
-
     public class BindItem
     {
         [JsonProperty("bindKey")]
@@ -95,7 +96,6 @@ namespace UAS_MES_NEW.PublicMethod
                 UserId = "邓帝森",
                 BindList = new List<BindItem>()
             };
-
             root.BindList.Add(new BindItem
             {
                 BindKey = "cartonSn",
@@ -204,5 +204,48 @@ namespace UAS_MES_NEW.PublicMethod
             string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
             Console.WriteLine(json);
         }
+
+        public static void SendData(string json)
+        {
+            string serverIP = "192.168.1.160";
+            int port = 21610;
+
+            try
+            {
+                // 创建TCP客户端并连接到服务器
+                TcpClient client = new TcpClient();
+                client.Connect(serverIP, port);
+                // 获取网络流
+                NetworkStream stream = client.GetStream();
+
+                // 要发送的请求报文(根据实际需求修改)
+                string requestMessage = "你的请求报文内容";
+                byte[] requestData = Encoding.UTF8.GetBytes(requestMessage);
+
+                // 发送请求
+                stream.Write(requestData, 0, requestData.Length);
+                Console.WriteLine($"已发送请求: {requestMessage}");
+
+                // 接收响应
+                byte[] buffer = new byte[1024];
+                int bytesRead = stream.Read(buffer, 0, buffer.Length);
+                string responseMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
+
+                Console.WriteLine($"收到响应: {responseMessage}");
+
+                // 关闭流和连接
+                stream.Close();
+                client.Close();
+            }
+            catch (SocketException ex)
+            {
+                Console.WriteLine($"网络错误: {ex.Message}");
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"错误: {ex.Message}");
+            }
+
+        }
     }
 }