From 9b99ea18258103b38aac6d0736b7de7a0c2ad784 Mon Sep 17 00:00:00 2001 From: liuwf Date: Mon, 15 Jul 2024 18:09:04 +0800 Subject: [PATCH] =?UTF-8?q?add-=E6=B7=BB=E5=8A=A0http=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Common/HttpHelper.cs | 116 +++++++++++++++++++++++ SlnMesnac.Common/SlnMesnac.Common.csproj | 1 + 2 files changed, 117 insertions(+) create mode 100644 SlnMesnac.Common/HttpHelper.cs diff --git a/SlnMesnac.Common/HttpHelper.cs b/SlnMesnac.Common/HttpHelper.cs new file mode 100644 index 0000000..3a7397b --- /dev/null +++ b/SlnMesnac.Common/HttpHelper.cs @@ -0,0 +1,116 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Ports; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Text.Json; +namespace SlnMesnac.Common +{ + public class HttpHelper + { + public static string Get(string serviceAddress) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + request.Method = "GET"; + request.ContentType = "text/html;charset=UTF-8"; + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + Stream myResponseStream = response.GetResponseStream(); + StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); + string retString = myStreamReader.ReadToEnd(); + myStreamReader.Close(); + myResponseStream.Close(); + return retString; + } + public static async Task GetAsync(string serviceAddress) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + request.Method = "GET"; + request.ContentType = "text/html;charset=UTF-8"; + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + Stream myResponseStream = response.GetResponseStream(); + StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); + string retString = await myStreamReader.ReadToEndAsync(); + myStreamReader.Close(); + myResponseStream.Close(); + return retString; + } + + public static string Post(string serviceAddress, string strContent = null) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + request.Method = "POST"; + request.ContentType = "application/json"; + //判断有无POST内容 + if (!string.IsNullOrWhiteSpace(strContent)) + { + using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) + { + dataStream.Write(strContent); + dataStream.Close(); + } + } + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + string encoding = response.ContentEncoding; + if (encoding.Length < 1) + { + encoding = "UTF-8"; //默认编码 + } + StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); + string retString = reader.ReadToEnd(); + return retString; + } + + public static async Task PostAsync(string serviceAddress, string strContent = null) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); + request.Method = "POST"; + request.ContentType = "application/json"; + //判断有无POST内容 + if (!string.IsNullOrWhiteSpace(strContent)) + { + using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) + { + dataStream.Write(strContent); + dataStream.Close(); + } + } + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + string encoding = response.ContentEncoding; + if (encoding.Length < 1) + { + encoding = "UTF-8"; //默认编码 + } + StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); + string retString = await reader.ReadToEndAsync(); + return retString; + } + + + /// + /// 叫料 + /// + /// + /// + public bool JL(string orderId) + { + + string url = $"http://10.100.72.10:8080/login"; + + var loginResult = Post(url,orderId);//发送用户名密码给API + + if(loginResult.Contains("200")) + { + return true; + } + else + { + return false; + } + } + } +} diff --git a/SlnMesnac.Common/SlnMesnac.Common.csproj b/SlnMesnac.Common/SlnMesnac.Common.csproj index 40b010f..8913ba7 100644 --- a/SlnMesnac.Common/SlnMesnac.Common.csproj +++ b/SlnMesnac.Common/SlnMesnac.Common.csproj @@ -9,6 +9,7 @@ +