using System; using System.Collections.Generic; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Net; using System.IO; using System.IO.Compression; using System.Text.RegularExpressions; using System.Web; using System.Security.Cryptography; using System.Collections.Specialized; using System.Reflection; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.Net.Http; using System.Threading.Tasks; namespace JJMediSys { class NetCom { private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //总是接受 } public static void WriteLog(string msg) { string filePath = AppDomain.CurrentDomain.BaseDirectory + "LisLog"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string logPath = AppDomain.CurrentDomain.BaseDirectory + "LisLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; try { using (StreamWriter sw = File.AppendText(logPath)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + msg + "\r\n"); sw.Flush(); sw.Close(); sw.Dispose(); } } catch (IOException e) { using (StreamWriter sw = File.AppendText(logPath)) { sw.WriteLine("异常:" + e.Message + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff") + "\r\n"); sw.Flush(); sw.Close(); sw.Dispose(); } } } public static void SetHeaderValue(WebHeaderCollection header, string name, string value) { var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic); if (property != null) { var collection = property.GetValue(header, null) as NameValueCollection; collection[name] = value; } } public static HttpWebResponse CreatePostHttpResponse(string url, string Input, Encoding charset) { HttpWebRequest request = null; //HTTPSQ请求 // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); // 1. 统一设置:跳过任何证书错误(开发/测试用) ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; request.Method = "POST"; //request.ContentLength = Input.Length; request.ContentType = "application/json"; //request.UserAgent = DefaultUserAgent; request.Timeout = 30000; request.Proxy = null; //如果需要POST数据 byte[] data = charset.GetBytes(Input); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } return request.GetResponse() as HttpWebResponse; } public string GetHttpResponse(string url, int Timeout) { string retString = ""; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; request.UserAgent = null; request.Timeout = Timeout; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); } catch (System.Exception ex) { WriteLog("Exception Error Message is " + ex.Message); } return retString; } public static HttpWebResponse CreateGetHttpResponse(string url, IDictionary parameters, Encoding charset) { StringBuilder builder = new StringBuilder(); builder.Append(url); if (parameters.Count > 0) { builder.Append("?"); int i = 0; foreach (var item in parameters) { if (i > 0) builder.Append("&"); builder.AppendFormat("{0}={1}", item.Key, item.Value); i++; } } HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString()); return (HttpWebResponse)req.GetResponse() as HttpWebResponse; } public string OutString; public bool SendAndRecv(string url, string Input,string PatID) { //OutString = "{\"code\": \"200\",\"msg\": \"成功\",\"data\": {\"patientId\": \"HN755155\",\"name\": \"马洪义\",\"age\": \"66\",\"sex\": \"男\",\"windowNo\": \"\",\"photo\": \"\",\"itemCount\": 7,\"userType\": \"0\",\"idCardNo\": \"\",\"phone\": \"\",\"healthCardNo\": \"\",\"doctor\": \"\",\"pojoList\": [{\"requestId\": \"2412062091\",\"patientId\": \"HN755155\",\"ward\": \"健康管理中心\",\"wardId\": \"812030\",\"bedNo\": \"\",\"tubeConvert\": \"YELLOW\",\"noteLabel\": \"静脉血\",\"mnemotests\": \"超级赛亚人检测\",\"station\": \"免疫室\",\"stationId\": \"817440\",\"state\": \"0\",\"datebz\": \"2024-12-06 10:11:42\",\"descbz\": \"\",\"pickupLocation\": \"门诊一层检验报告领取处\",\"pickupTime\": \"下个工作日16:30后\",\"groupId\": \"\",\"labelCount\": \"1\",\"mnemoitems\": \"防御力检测;\"},{\"requestId\": \"2412062072\",\"patientId\": \"HN755155\",\"ward\": \"健康管理中心\",\"wardId\": \"812030\",\"bedNo\": \"\",\"tubeConvert\": \"YELLOW\",\"noteLabel\": \"静脉血\",\"mnemotests\": \"波塞冬测试\",\"station\": \"免疫室\",\"stationId\": \"817440\",\"state\": \"0\",\"datebz\": \"2024-12-06 10:11:42\",\"descbz\": \"\",\"pickupLocation\": \"门诊一层检验报告领取处\",\"pickupTime\": \"下工作日16:30后(周末延1个工作日)\",\"groupId\": \"\",\"labelCount\": \"1\",\"mnemoitems\": \"乙型肝炎病毒表面抗体(发光法);乙型肝炎病毒E抗原(发光法);乙型肝炎病毒E抗体(发光法);乙型肝炎病毒核心抗体(发光法);艾滋病抗原、抗体测定(发光法);丙型肝炎病毒抗体(发光法);乙型肝炎病毒表面抗原(发光法);梅毒血清特异抗体测定(发光法);\"}]}}"; //string sql1 = "Insert into lis_log (PatID,Req,Res) values('" + PatID + "','" + Input + "','" + OutString + "')"; //try //{ // int Ret = DoDBMySql.ExecuteSql(sql1); //} //catch (Exception ex) //{ //} //return true; //WriteLog(Input); Encoding encoding = Encoding.GetEncoding("utf-8"); OutString = ""; try { HttpWebResponse response = CreatePostHttpResponse(url, Input, encoding); Stream stream = response.GetResponseStream(); //获取响应的字符串流 StreamReader sr = new StreamReader(stream); //创建一个stream读取流 OutString = sr.ReadToEnd(); //从头读到尾,放到字符串html string logstr = string.Format("url={0}\r\n Req={1}\r\nRes={2}", url, Input, OutString); WriteLog(logstr); string sql = "Insert into lis_log (PatID,Req,Res) values('" + PatID + "','" + Input + "','" + OutString + "')"; try { int Ret = DoDBMySql.ExecuteSql(sql); } catch (Exception ex) { } return true; } catch (Exception e) { WriteLog("Exception Error Message is " + e.Message); string sql = "Insert into lis_log (PatID,Req,Res) values('" + PatID + "','" + Input + "','" + e.Message + "')"; try { int Ret = DoDBMySql.ExecuteSql(sql); } catch (Exception ex) { } return false; } } public static async Task SendHisComplete(string PatID) { WriteLog($"进入诊结"); try { using (var client = new HttpClient()) { // 准备表单数据 var formData = new Dictionary { { "out_code", PatID } }; // 发送 POST 请求 HttpResponseMessage response = await client.PostAsync( "http://10.10.8.244:10005/api/doctor/his/completed", // 替换为你的目标 URL new FormUrlEncodedContent(formData) ); // 确保成功 response.EnsureSuccessStatusCode(); // 读取响应内容 bool ExcutResult = false; string Outmsg = await response.Content.ReadAsStringAsync(); WriteLog($"诊结请求[{PatID}] 应答:[{Outmsg}]"); string sql = "Insert into lis_log (PatID,Req,Res) values('" + PatID + "','" + PatID + "','" + Outmsg + "')"; JObject jObject = (JObject)JsonConvert.DeserializeObject(Outmsg); if (jObject["status"].ToString().Equals("200")) { WriteLog("提交诊结成功 , 回参:" + Outmsg); ExcutResult = true; } else { WriteLog("提交诊结失败, 回参:" + Outmsg); } try { int Ret = DoDBMySql.ExecuteSql(sql); } catch (Exception e) { } return ExcutResult; } } catch (WebException ex) { WriteLog("提交诊结异常:" + ex.Message); using (var reader = new StreamReader(ex.Response?.GetResponseStream())) { string Outmsg = reader.ReadToEnd(); WriteLog("提交诊结异常:" + ex.Message+", 回参:"+ Outmsg); string sql = "Insert into lis_log (PatID,Req,Res) values('" + PatID + "','" + PatID + "','" + Outmsg + "')"; try { int Ret = DoDBMySql.ExecuteSql(sql); } catch (Exception e) { } return false; } } } } }