HttpServices.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. namespace prBrowser.Weblogic
  10. {
  11. class HttpServices
  12. {
  13. public string HISHTTPServices(string data, string ip, string url, string hisauthorization)
  14. {
  15. string result = Post(data, ip, url, hisauthorization);
  16. return result;
  17. }
  18. #region Get请求
  19. public string Get(string hisip, string uri)
  20. {
  21. //先根据用户请求的uri构造请求地址
  22. string serviceUrl = string.Format("{0}{1}", hisip, uri);
  23. //创建Web访问对 象
  24. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
  25. //通过Web访问对象获取响应内容
  26. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  27. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  28. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  29. //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  30. string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  31. reader.Close();
  32. myResponse.Close();
  33. return returnXml;
  34. }
  35. #endregion
  36. #region Post请求
  37. public string Post(string data, string hisip, string hisurl, string hisauthorization)
  38. {
  39. //先根据用户请求的uri构造请求地址
  40. string serviceUrl = string.Format("{0}{1}", hisip, hisurl);
  41. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  42. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  43. //创建Web访问对象
  44. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
  45. //
  46. //把用户传过来的数据转成“UTF-8”的字节流
  47. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  48. myRequest.Method = "POST";
  49. myRequest.ContentLength = buf.Length;
  50. myRequest.ContentType = "application/json";
  51. myRequest.Headers.Add("Authorization", hisauthorization);
  52. //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");
  53. //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");
  54. myRequest.MaximumAutomaticRedirections = 1;
  55. myRequest.AllowAutoRedirect = true;
  56. //发送请求
  57. Stream stream = myRequest.GetRequestStream();
  58. stream.Write(buf, 0, buf.Length);
  59. stream.Close();
  60. //获取接口返回值
  61. //通过Web访问对象获取响应内容
  62. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  63. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  64. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  65. //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  66. string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  67. reader.Close();
  68. myResponse.Close();
  69. return returnXml;
  70. }
  71. #endregion
  72. }
  73. }