HttpServices.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Web;
  8. using Newtonsoft.Json.Linq;
  9. using FileInfoClass;
  10. using Ini;
  11. using System.Threading.Tasks;
  12. namespace HttpServices
  13. {
  14. public class RestClient
  15. {
  16. FileClass fileclass = new FileClass();
  17. private string logPath = "";
  18. public async Task<string> HISHTTPServices(string data, string ip = "", string url = "", string authorization = "")
  19. {
  20. IniFile ini = new IniFile();
  21. string hisip = ip;
  22. string hisurl = url;
  23. string hisauthorization = authorization;
  24. if (ip == "")
  25. {
  26. hisip = ini.IniReadValue("HIS", "ip");
  27. }
  28. if (url == "")
  29. {
  30. hisurl = ini.IniReadValue("HIS", "url");
  31. }
  32. if (authorization == "")
  33. {
  34. hisauthorization = ini.IniReadValue("HIS", "authorization");
  35. }
  36. if (hisip.IndexOf(hisurl) > -1) {
  37. hisurl = "";
  38. }
  39. logPath = ini.IniReadValue("LOG", "path");
  40. string DateTimeinfo = DateTime.Now.TimeOfDay.ToString();
  41. fileclass.WriteLogFile(logPath, DateTimeinfo + "-->" + hisip+"/"+ hisurl);
  42. fileclass.WriteLogFile(logPath, " 入参: " + data);
  43. //RestClient client = new RestClient();
  44. string result = await Post(data, hisip, hisurl, hisauthorization);
  45. fileclass.WriteLogFile(logPath, DateTimeinfo + " 返参: " + result);
  46. return result;
  47. }
  48. #region Get请求
  49. public string Get(string hisip, string uri)
  50. {
  51. //先根据用户请求的uri构造请求地址
  52. string serviceUrl = string.Format("{0}/{1}", hisip, uri);
  53. //创建Web访问对 象
  54. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
  55. //通过Web访问对象获取响应内容
  56. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  57. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  58. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  59. //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  60. string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  61. reader.Close();
  62. myResponse.Close();
  63. return returnXml;
  64. }
  65. #endregion
  66. #region Post请求
  67. public async Task<string> Post(string data, string hisip, string hisurl, string hisauthorization)
  68. {
  69. return await Task.Run(() =>
  70. {
  71. string errorCode = "-1";
  72. string errorMessage = "";
  73. try
  74. {
  75. //先根据用户请求的uri构造请求地址
  76. string serviceUrl = string.Format("{0}/{1}", hisip, hisurl);
  77. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  78. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  79. //创建Web访问对象
  80. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
  81. //把用户传过来的数据转成“UTF-8”的字节流
  82. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  83. myRequest.Method = "POST";
  84. myRequest.ContentLength = buf.Length;
  85. myRequest.ContentType = "application/json";
  86. if (hisauthorization != null)
  87. {
  88. myRequest.Headers.Add("Authorization", hisauthorization);
  89. }
  90. //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");
  91. //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");
  92. //myRequest.Headers.Add("Authorization", hisauthorization);
  93. myRequest.MaximumAutomaticRedirections = 1;
  94. myRequest.AllowAutoRedirect = true;
  95. //发送请求
  96. Stream stream = myRequest.GetRequestStream();
  97. stream.Write(buf, 0, buf.Length);
  98. stream.Close();
  99. //获取接口返回值
  100. //通过Web访问对象获取响应内容
  101. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  102. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  103. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  104. //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  105. string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  106. reader.Close();
  107. myResponse.Close();
  108. return returnXml;
  109. }
  110. catch (Exception ex2)
  111. {
  112. errorCode = "-1";
  113. errorMessage = ex2.Message;
  114. }
  115. JObject returnjObject = new JObject();
  116. returnjObject.Add("errorCode", errorCode);
  117. returnjObject.Add("errorMessage", errorMessage);
  118. return returnjObject.ToString();
  119. });
  120. }
  121. #endregion
  122. }
  123. }