using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using HTTPServerLib; using System.IO; using System.Xml; using System.Windows.Forms; using System.Reflection.Emit; using 心电图绘制; using Newtonsoft.Json; using 心电图绘制.Properties; using System.Drawing; using Newtonsoft.Json.Linq; namespace _1200Gxml心电图绘制 { public class ExampleServer : HTTPServerLib.HttpServer { private Control _logControl; ConsoleLogger logger; print print = new print(); /// /// 报告样式类型 /// public int reportMode; /// /// 报告样式类型名称 /// public string strreportMode = string.Empty; /// /// 走速 /// public int cBoxSpeed = 0; /// /// 振幅 /// public int cBoxAmplitude = 0; /// /// 构造函数 /// /// IP地址 /// 端口号 public ExampleServer(string ipAddress, int port, Control _logControl) : base(ipAddress, port) { logger = new ConsoleLogger(_logControl); } public override void OnPost(HttpRequest request, HttpResponse response) { //获取客户端传递的参数 // string data = request.Params == null ? "" : string.Join(";", request.Params.Select(x => x.Key + "=" + x.Value).ToArray()); string strMQName = string.Empty; //LogHelper.Info("接收到请求", ""); string data = ""; try { if (request != null && request.Params != null) { //注意调整URL顺序 if (request.URL.StartsWith(@"/printecg_image")) { JObject paramsObj = null; string[] array = request.Params.Select(x => x.Value).ToArray(); if (array.Length > 0 && !string.IsNullOrWhiteSpace(array[0])) try { //LogHelper.Info("请求参数", array[0].Trim()); paramsObj = JsonConvert.DeserializeObject(array[0].Trim()); } catch (Exception ex) { paramsObj = null; } if (paramsObj != null) { if (paramsObj.ContainsKey("filepath") && !string.IsNullOrWhiteSpace(paramsObj["filepath"] == null ? null : paramsObj["filepath"].ToString().Trim())) { try { Log("开始处理请求:" + paramsObj["regid"].ToString().Trim()); print.GetEcgViewDataImage_img(paramsObj); if (paramsObj["type"].ToString().Trim().Equals(@"1")) { data = print.imagebase; } else { data = print.filename; } Log("请求处理完成:" + paramsObj["regid"].ToString().Trim()); try { if (false) print.FlySubmit3(new PatientInfo() { regid = paramsObj["regid"].ToString().Trim(), examid = paramsObj["examid"].ToString().Trim(), diagContent = paramsObj["diagContent"] == null ? "" : paramsObj["diagContent"].ToString().Trim(), orgid = paramsObj["orgid"] == null ? "" : paramsObj["orgid"].ToString().Trim(), orgname = paramsObj["orgname"] == null ? "" : paramsObj["orgname"].ToString().Trim(), doctorName = paramsObj["doctorName"] == null ? "" : paramsObj["doctorName"].ToString().Trim(), isAbnormal = paramsObj["isAbnormal"] == null ? "" : paramsObj["isAbnormal"].ToString().Trim() }); } catch (Exception ex) { } } catch (Exception ex) { data = "生成数据文件异常"; } } else data = "心电数据为空"; } else data = "解析数据不正确"; } else if (request.URL.StartsWith(@"/printecg")) { string[] array = request.Params.Select(x => x.Value).ToArray(); if (array.Length > 0) { //LogHelper.Info("请求参数", array[0]); PatientInfo patientInfo = JsonConvert.DeserializeObject(array[0]); print.cBoxSpeed = cBoxSpeed; print.cBoxAmplitude = cBoxAmplitude; print.strreportMode = strreportMode; print.reportMode = reportMode; if (patientInfo != null) { if (!string.IsNullOrWhiteSpace(patientInfo.filepath)) { try { Log("开始处理请求:" + patientInfo.regid); Image image = null; if (!string.IsNullOrWhiteSpace(patientInfo.image)) { image = UniversalFun.ConvertFromBase64String(patientInfo.image); } string ecgDataJsonStr = UniversalFun.GetTextFileContent(patientInfo.filepath); print.GetEcgViewDataImage(image, "", patientInfo, patientInfo.type, ecgDataJsonStr); if (patientInfo.type == 1) { data = print.imagebase; } else { data = print.filename; } Log("请求处理完成:" + patientInfo.regid); try { if (false) { patientInfo.diagContent = patientInfo.diagContent == null ? "" : patientInfo.diagContent.Trim(); patientInfo.orgid = patientInfo.orgid == null ? "" : patientInfo.orgid.Trim(); patientInfo.orgname = patientInfo.orgname == null ? "" : patientInfo.orgname.Trim(); patientInfo.doctorName = patientInfo.doctorName == null ? "" : patientInfo.doctorName.Trim(); patientInfo.isAbnormal = patientInfo.isAbnormal == null ? "" : patientInfo.isAbnormal.Trim(); print.FlySubmit3(patientInfo); } } catch (Exception ex) { } } catch (Exception ex) { data = "生成数据文件异常"; } } else { data = "心电数据为空"; } } else { data = "解析数据不正确"; } } } else { data = "非指定请求方法"; } } else { data = "请求参数为空"; } } catch (Exception e) { //LogHelper.Info("OnPost Error:", e.Message); } //设置返回信息 string content = data; //string.Format("0000Successful!{0}", data); //构造响应报文 response.SetContent(content); response.Content_Encoding = "utf-8"; response.StatusCode = "200"; response.Content_Type = "application/json; charset=UTF-8"; response.Server = "ExampleServer"; // LogHelper.Info("返回值:" ,content); //发送响应 response.Send(); } public override void OnGet(HttpRequest request, HttpResponse response) { //LogHelper.Info("请求到接口", ""); //当文件不存在时应返回404状态码 string requestURL = request.URL; requestURL = requestURL.Replace("/", @"\").Replace("\\..", "").TrimStart('\\'); /* string requestFile = Path.Combine(ServerRoot, requestURL); //根据不同业务进行修改 //判断地址中是否存在扩展名 string extension = Path.GetExtension(requestFile); //根据有无扩展名按照两种不同链接进行处 if (extension != "") { //从文件中返回HTTP响应 response = LoadFromFile(response, requestFile); } else { //目录存在且不存在index页面时时列举目录 if (Directory.Exists(requestFile) && !File.Exists(requestFile + "\\index.html")) { requestFile = Path.Combine(ServerRoot, requestFile); var content = ListDirectory(requestFile, requestURL); response = response.SetContent(content, Encoding.UTF8); response.Content_Type = "text/html; charset=UTF-8"; } else { //加载静态HTML页面 requestFile = Path.Combine(requestFile, "index.html"); response = LoadFromFile(response, requestFile); response.Content_Type = "text/html; charset=UTF-8"; } } */ //发送HTTP响应 StringBuilder sb = new StringBuilder(); sb.Append("接收到接口数据"); response.SetContent(sb.ToString()); response.StatusCode = "200"; response.Content_Type = "text/html"; response.Server = "ExampleServer"; response.Send(); } public override void OnDefault(HttpRequest request, HttpResponse response) { } private async void GetFilePathUrl(string filepath) { } /// /// 从文件返回一个HTTP响应 /// /// 文件名 private HttpResponse LoadFromFile(HttpResponse response, string fileName) { //获取文件扩展名以判断内容类型 string extension = Path.GetExtension(fileName); //获取当前内容类型 string contentType = GetContentType(extension); //如果文件不存在则返回404否则读取文件内容 if (!File.Exists(fileName)) { response.SetContent("

404 - Not Found

"); response.StatusCode = "404"; response.Content_Type = "text/html"; response.Server = "ExampleServer"; } else { response.SetContent(File.ReadAllBytes(fileName)); response.StatusCode = "200"; response.Content_Type = contentType; response.Server = "ExampleServer"; } //返回数据 return response; } private string ConvertPath(string[] urls) { string html = string.Empty; int length = ServerRoot.Length; foreach (var url in urls) { var s = url.StartsWith("..") ? url : url.Substring(length).TrimEnd('\\'); html += String.Format("
  • {0}
  • ", s); } return html; } private string ListDirectory(string requestDirectory, string requestURL) { //列举子目录 var folders = requestURL.Length > 1 ? new string[] { "../" } : new string[] { }; folders = folders.Concat(Directory.GetDirectories(requestDirectory)).ToArray(); var foldersList = ConvertPath(folders); //列举文件 var files = Directory.GetFiles(requestDirectory); var filesList = ConvertPath(files); //构造HTML StringBuilder builder = new StringBuilder(); builder.Append(string.Format("{0}", requestDirectory)); builder.Append(string.Format("

    {0}


    ", requestURL, filesList, foldersList)); return builder.ToString(); } } }