HttpServices.cs 5.0 KB

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