星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Security;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Text;
  8. using System.Threading;
  9. namespace Common.system
  10. {
  11. /// <summary>
  12. /// HttpHelper
  13. /// 创建人:赵耀
  14. /// </summary>
  15. public class HttpHelper
  16. {
  17. public static void init_Request(ref System.Net.HttpWebRequest request)
  18. {
  19. request.Accept = "text/json,*/*;q=0.5";
  20. request.Headers.Add("Accept-Charset", "utf-8;q=0.7,*;q=0.7");
  21. request.Headers.Add("Accept-Encoding", "gzip, deflate, x-gzip, identity; q=0.9");
  22. request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
  23. request.Timeout = 8000;
  24. }
  25. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  26. {
  27. return true; //总是接受
  28. }
  29. public static System.Net.HttpWebRequest GetHttpWebRequest(string url)
  30. {
  31. HttpWebRequest request = null;
  32. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  33. {
  34. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  35. request = WebRequest.Create(url) as HttpWebRequest;
  36. request.ProtocolVersion = HttpVersion.Version10;
  37. }
  38. else
  39. {
  40. request = WebRequest.Create(url) as HttpWebRequest;
  41. }
  42. return request;
  43. }
  44. public static WebResponse Download(string downloadUrl, long from, long to, string method)
  45. {
  46. for (int i = 0; i < 10; i++)
  47. {
  48. try
  49. {
  50. var request = HttpHelper.GetHttpWebRequest(downloadUrl);
  51. HttpHelper.init_Request(ref request);
  52. request.Accept = "text/json,*/*;q=0.5";
  53. request.AddRange(from, to);
  54. request.Headers.Add("Accept-Charset", "utf-8;q=0.7,*;q=0.7");
  55. request.Headers.Add("Accept-Encoding", "gzip, deflate, x-gzip, identity; q=0.9");
  56. request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
  57. request.Timeout = 120000;
  58. request.Method = method;
  59. request.KeepAlive = false;
  60. request.ContentType = "application/json; charset=utf-8";
  61. return request.GetResponse();
  62. }
  63. catch (Exception)
  64. {
  65. Thread.Sleep(100);
  66. }
  67. }
  68. throw new Exception("已断开网络!请检查网络连接后重试下载!");
  69. }
  70. public static string Get(string url, IDictionary<string, string> param)
  71. {
  72. var paramBuilder = new List<string>();
  73. foreach (var item in param)
  74. {
  75. paramBuilder.Add(string.Format("{0}={1}", item.Key, item.Value));
  76. }
  77. url = string.Format("{0}?{1}", url.TrimEnd('?'), string.Join(",", paramBuilder.ToArray()));
  78. return Get(url);
  79. }
  80. public static string Get(string url)
  81. {
  82. try
  83. {
  84. var request = GetHttpWebRequest(url);
  85. if (request != null)
  86. {
  87. string retval = null;
  88. init_Request(ref request);
  89. using (var Response = request.GetResponse())
  90. {
  91. using (var reader = new System.IO.StreamReader(Response.GetResponseStream(), System.Text.Encoding.UTF8))
  92. {
  93. retval = reader.ReadToEnd();
  94. }
  95. }
  96. return retval;
  97. }
  98. }
  99. catch
  100. {
  101. }
  102. return null;
  103. }
  104. public static string Post(string url, string data)
  105. {
  106. try
  107. {
  108. var request = GetHttpWebRequest(url);
  109. if (request != null)
  110. {
  111. string retval = null;
  112. init_Request(ref request);
  113. request.Method = "POST";
  114. request.ServicePoint.Expect100Continue = false;
  115. request.ContentType = "application/json; charset=utf-8";
  116. request.Timeout = 800;
  117. var bytes = System.Text.UTF8Encoding.UTF8.GetBytes(data);
  118. request.ContentLength = bytes.Length;
  119. using (var stream = request.GetRequestStream())
  120. {
  121. stream.Write(bytes, 0, bytes.Length);
  122. }
  123. using (var response = request.GetResponse())
  124. {
  125. using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
  126. {
  127. retval = reader.ReadToEnd();
  128. }
  129. }
  130. return retval;
  131. }
  132. }
  133. catch
  134. {
  135. }
  136. return null;
  137. }
  138. }
  139. }