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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  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. /// <summary>
  139. /// Post Http请求
  140. /// </summary>
  141. /// <param name="url">请求地址</param>
  142. /// <param name="postData">传输数据</param>
  143. /// <param name="timeout">超时时间</param>
  144. /// <param name="contentType">媒体格式</param>
  145. /// <param name="encode">编码</param>
  146. /// <returns>泛型集合</returns>
  147. public static T PostAndRespSignle<T>(string url, int timeout = 5000, string postData = "", string contentType = "application/json;", string encode = "UTF-8") where T : class
  148. {
  149. if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null)
  150. {
  151. // webRequest.Headers.Add("Authorization", "Bearer " + "SportApiAuthData");
  152. HttpWebResponse webResponse = null;
  153. Stream responseStream = null;
  154. Stream requestStream = null;
  155. StreamReader streamReader = null;
  156. try
  157. {
  158. string respstr = GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);
  159. return JsonHelper.JsonToObj<T>(respstr);
  160. }
  161. catch (Exception)
  162. {
  163. }
  164. finally
  165. {
  166. if (responseStream != null)
  167. {
  168. responseStream.Dispose();
  169. }
  170. if (webResponse != null)
  171. {
  172. webResponse.Close();
  173. }
  174. if (requestStream != null)
  175. {
  176. requestStream.Dispose();
  177. }
  178. if (streamReader != null)
  179. {
  180. streamReader.Dispose();
  181. }
  182. }
  183. }
  184. return default(T);
  185. }
  186. private static string GetStreamReader(string url, Stream responseStream, Stream requestStream, StreamReader streamReader, WebResponse webResponse, int timeout, string encode, string postData, string contentType)
  187. {
  188. byte[] data = Encoding.GetEncoding(encode).GetBytes(postData);
  189. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  190. webRequest.Method = "POST";
  191. webRequest.ContentType = contentType + ";" + encode;
  192. webRequest.ContentLength = data.Length;
  193. webRequest.Timeout = timeout;
  194. requestStream = webRequest.GetRequestStream();
  195. requestStream.Write(data, 0, data.Length);
  196. webResponse = (HttpWebResponse)webRequest.GetResponse();
  197. responseStream = webResponse.GetResponseStream();
  198. if (responseStream == null) { return ""; }
  199. streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode));
  200. return streamReader.ReadToEnd();
  201. }
  202. }
  203. }