星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HttpHelper.cs 5.2KB

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