DoHttpHelp.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using NIU.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace CP.Common
  11. {
  12. public class DoHttpHelp
  13. {
  14. public static string PostHttp(string url, string methodType, Dictionary<string, string> headers, string body = null)
  15. {
  16. HttpWebRequest request = null;
  17. StreamWriter sw = null;
  18. StreamReader s = null;
  19. string ret;
  20. try
  21. {
  22. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;//解决:请求被中止: 未能创建 SSL/TLS 安全通道
  23. request = (HttpWebRequest)WebRequest.Create(url);//请求
  24. request.Method = methodType;
  25. if (headers != null && headers.Count > 0)
  26. for (var i = 0; i < headers.Count; i++)
  27. request.Headers.Add(headers.ElementAt(i).Key, headers.ElementAt(i).Value);
  28. request.KeepAlive = true;
  29. //request.CookieContainer = cook;
  30. request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  31. //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
  32. request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36";
  33. request.ServicePoint.Expect100Continue = false;
  34. request.ServicePoint.ConnectionLimit = int.MaxValue;
  35. if (!string.IsNullOrEmpty(body))
  36. {
  37. sw = new StreamWriter(request.GetRequestStream());//获取写入流
  38. sw.Write(body);
  39. sw.Flush(); //强制写入
  40. }
  41. WebResponse response = request.GetResponse(); //获取响应
  42. s = new StreamReader(response.GetResponseStream());//获取响应流
  43. ret = s.ReadToEnd(); //读取数据
  44. }
  45. catch (Exception ex)
  46. {
  47. throw ex;
  48. }
  49. finally
  50. {
  51. if (s != null) s.Close();
  52. if (sw != null) sw.Close();
  53. if (request != null) request.Abort();
  54. s = null;
  55. sw = null;
  56. request = null;
  57. }
  58. return ret;
  59. }
  60. /// <summary>
  61. /// 清理webapi开奖号数据缓存
  62. /// </summary>
  63. /// <param name="ename"></param>
  64. /// <returns></returns>
  65. public static bool UpdateWebApi(string ename)
  66. {
  67. string apiUrl = ConfigurationManager.AppSettings["api55128"].ToString();
  68. string wwwUrl = ConfigurationManager.AppSettings["www55128"].ToString();
  69. apiUrl += ename;
  70. wwwUrl += ename;
  71. PostHttp(wwwUrl, "Get", null);
  72. PostHttp(apiUrl, "Get", null);
  73. return true;
  74. }
  75. /// <summary>
  76. /// 清理webapi开奖号数据缓存
  77. /// </summary>
  78. /// <param name="ename"></param>
  79. /// <returns></returns>
  80. public static bool UpdateWebApi_admin(string ename)
  81. {
  82. string apiUrl = ConfigurationManager.AppSettings["api55128"].ToString();
  83. string wwwUrl = ConfigurationManager.AppSettings["www55128"].ToString();
  84. apiUrl += ename;
  85. wwwUrl += ename;
  86. var z = PostHttp(wwwUrl, "Get", null);
  87. var a = PostHttp(apiUrl, "Get", null);
  88. if (z == "0" && a.Contains("0"))
  89. return true;
  90. else
  91. throw new OperationExceptionFacade("清除缓存失败");
  92. }
  93. public static string PostHttpForAdmin(string url, string methodType, Dictionary<string, string> headers, string body = null)
  94. {
  95. HttpWebRequest request = null;
  96. StreamWriter sw = null;
  97. StreamReader s = null;
  98. string ret;
  99. try
  100. {
  101. request = (HttpWebRequest)WebRequest.Create(url);//请求
  102. request.Method = methodType;
  103. if (headers != null && headers.Count > 0)
  104. for (var i = 0; i < headers.Count; i++)
  105. request.Headers.Add(headers.ElementAt(i).Key, headers.ElementAt(i).Value);
  106. request.KeepAlive = true;
  107. //request.CookieContainer = cook;
  108. request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  109. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
  110. request.ServicePoint.Expect100Continue = false;
  111. request.ServicePoint.ConnectionLimit = int.MaxValue;
  112. if (!string.IsNullOrEmpty(body))
  113. {
  114. byte[] bytes = Encoding.Default.GetBytes(body);
  115. Stream sendStream = request.GetRequestStream();
  116. sendStream.Write(bytes, 0, bytes.Length);
  117. sendStream.Close();
  118. }
  119. WebResponse response = request.GetResponse(); //获取响应
  120. s = new StreamReader(response.GetResponseStream());//获取响应流
  121. ret = s.ReadToEnd(); //读取数据
  122. }
  123. catch (Exception ex)
  124. {
  125. throw ex;
  126. }
  127. finally
  128. {
  129. if (s != null) s.Close();
  130. if (sw != null) sw.Close();
  131. if (request != null) request.Abort();
  132. s = null;
  133. sw = null;
  134. request = null;
  135. }
  136. return ret;
  137. }
  138. }
  139. }