using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; using Newtonsoft.Json.Linq; using Ini; using FileInfoClass; using Newtonsoft.Json; namespace HttpServices { public class RestClient { FileClass fileclass = new FileClass(); private string logPath = ""; public string HISHTTPServices(string data, string ip = "", string url = "", string authorization = "") { IniFile ini = new IniFile(); string hisip = ip; string hisurl = url; string hisauthorization = authorization; if (ip == "") { hisip = ini.IniReadValue("HIS", "ip"); } if (url == "") { hisurl = ini.IniReadValue("HIS", "url"); } if (authorization == "") { hisauthorization = ini.IniReadValue("HIS", "authorization"); } if (hisip.IndexOf(hisurl) > -1) { hisurl = ""; } RestClient client = new RestClient(); string result = client.Post(data, hisip, hisurl, hisauthorization); return result; } public string InsuPTHTTPServices(string methodName, string data) { IniFile ini = new IniFile(); logPath = ini.IniReadValue("LOG", "path"); fileclass.WriteLogFile(logPath, DateTime.Now.TimeOfDay.ToString() + "-->" + methodName + " in: " + data); string hisip = ini.IniReadValue("InsuPT", "ip"); string hisurl = ini.IniReadValue("InsuPT", "url"); string hisauthorization = ini.IniReadValue("InsuPT", "authorization"); //fileclass.WriteLogFile(logPath, DateTime.Now.TimeOfDay.ToString() + "-->" + methodName + " url: " + hisip+"/"+ hisurl+" "+ hisauthorization); RestClient client = new RestClient(); string result = client.Post(data, hisip, hisurl, hisauthorization); fileclass.WriteLogFile(logPath, DateTime.Now.TimeOfDay.ToString() + "-->" + methodName + " out: " + result); return result; } public string HTTPHeadersServices(string data, string ip, string url, string authorization, string headers) { string result = Post(data, ip, url, authorization, headers); return result; } #region Get请求 public string Get(string hisip, string uri) { //先根据用户请求的uri构造请求地址 string serviceUrl = string.Format("{0}/{1}", hisip, uri); //创建Web访问对 象 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); //通过Web访问对象获取响应内容 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法 string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾 reader.Close(); myResponse.Close(); return returnXml; } #endregion #region Post请求 public string Post(string data, string hisip, string hisurl, string hisauthorization, string headers = null) { //先根据用户请求的uri构造请求地址 string serviceUrl = string.Format("{0}/{1}", hisip, hisurl); ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; //创建Web访问对象 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); // //把用户传过来的数据转成“UTF-8”的字节流 byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data); string returnXml = ""; myRequest.Method = "POST"; myRequest.ContentLength = buf.Length; myRequest.ContentType = "application/json; charset=utf-8"; myRequest.Accept = "application/json; charset=utf-8"; if (hisauthorization != null) { myRequest.Headers.Add("Authorization", hisauthorization); } if ((headers != null) && (headers != "")) { JObject headersObj = JsonConvert.DeserializeObject(headers); foreach (var header in headersObj) { myRequest.Headers.Add(header.Key, header.Value.ToString()); } } //myRequest.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"); //myRequest.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"); //myRequest.Headers.Add("Authorization", hisauthorization); myRequest.MaximumAutomaticRedirections = 1; myRequest.AllowAutoRedirect = true; //发送请求 Stream stream = myRequest.GetRequestStream(); stream.Write(buf, 0, buf.Length); stream.Close(); //获取接口返回值 //通过Web访问对象获取响应内容 // 获取响应并处理 using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse()) //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快 using (Stream responseStream = myResponse.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) { returnXml = reader.ReadToEnd(); WebHeaderCollection Headers = myResponse.Headers; // 将响应头赋值给返回对象 string headersInfo = HeadersToJson(Headers); JObject headersInfoObj = JsonConvert.DeserializeObject(headersInfo); var outlist = new { body = returnXml, headers = headersInfoObj }; returnXml = JsonConvert.SerializeObject(outlist); } return returnXml; } public static string HeadersToJson(WebHeaderCollection headers) { if (headers == null) return "{}"; var headerDict = new Dictionary(); foreach (string key in headers.AllKeys) { // 注意:同一个 header 可能有多个值(如 Set-Cookie),这里取第一个或合并 headerDict[key] = headers.Get(key); // Get(key) 返回所有值(逗号分隔) } return JsonConvert.SerializeObject(headerDict, Formatting.None); } #endregion } }