http_helper.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.IO;
  11. using System.Threading;
  12. using System.Drawing.Imaging;
  13. using System.Security.Cryptography.X509Certificates;
  14. using System.Net.Security;
  15. namespace HXX.Scanner.Biz
  16. {
  17. /// <summary>
  18. /// http工具
  19. /// </summary>
  20. public partial class http_helper
  21. {
  22. /// <summary>
  23. /// 基工具
  24. /// </summary>
  25. private static http_helper Net = new http_helper();
  26. /// <summary>
  27. /// 基cookie
  28. /// </summary>
  29. private CookieContainer cookies = new CookieContainer();
  30. /// <summary>
  31. /// post
  32. /// </summary>
  33. /// <param name="url"></param>
  34. /// <param name="content"></param>
  35. /// <returns></returns>
  36. public static string Post(string url, string content)
  37. {
  38. return Net.HTTP_POST(url, content, "", "");
  39. }
  40. /// <summary>
  41. /// post
  42. /// </summary>
  43. /// <param name="url"></param>
  44. /// <param name="content"></param>
  45. /// <param name="headTitle"></param>
  46. /// <param name="headContent"></param>
  47. /// <returns></returns>
  48. public static string Post(string url, string content, string headTitle, string headContent)
  49. {
  50. return Net.HTTP_POST(url, content, headTitle, headContent);
  51. }
  52. /// <summary>
  53. /// get
  54. /// </summary>
  55. /// <param name="url"></param>
  56. /// <returns></returns>
  57. public static string Get(string url)
  58. {
  59. return Net.HTTP_GET(url, 100, "", "");
  60. }
  61. /// <summary>
  62. /// get
  63. /// </summary>
  64. /// <param name="url"></param>
  65. /// <param name="headTitle"></param>
  66. /// <param name="headContent"></param>
  67. /// <returns></returns>
  68. public static string Get(string url, string headTitle, string headContent)
  69. {
  70. return Net.HTTP_GET(url, 100, headTitle, headContent);
  71. }
  72. /// <summary>
  73. /// get
  74. /// </summary>
  75. /// <param name="url"></param>
  76. /// <param name="timeout"></param>
  77. /// <returns></returns>
  78. public static string Get(string url, int timeout)
  79. {
  80. return Net.HTTP_GET(url, timeout, "", "");
  81. }
  82. /// <summary>
  83. /// delete
  84. /// </summary>
  85. /// <param name="url"></param>
  86. /// <returns></returns>
  87. public static string Delete(string url)
  88. {
  89. return Net.HTTPSDelete(url);
  90. }
  91. /// <summary>
  92. /// post
  93. /// </summary>
  94. /// <param name="url"></param>
  95. /// <param name="Content"></param>
  96. /// <param name="headTitle"></param>
  97. /// <param name="headContent"></param>
  98. /// <returns></returns>
  99. public string HTTP_POST(string url, string Content, string headTitle, string headContent)
  100. {
  101. try
  102. {
  103. //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
  104. //ServicePointManager.ServerCertificateValidationCallback =
  105. // new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  106. byte[] bContent = Encoding.UTF8.GetBytes(Content);
  107. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  108. req.Method = "POST";
  109. req.ContentLength = bContent.Length;
  110. string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  111. //req.ContentType = "text/xml";
  112. req.ContentType = "application/json;charset=utf-8";
  113. //req.ContentType = "application/x-www-form-urlencoded";
  114. //req.ContentType = "multipart/form-data; boundary=" + boundary;
  115. //req.CookieContainer = cookies;
  116. //req.ProtocolVersion = HttpVersion.Version10;
  117. //var nn = req.ProtocolVersion;
  118. if (headTitle.Length > 0)
  119. {
  120. req.Headers.Add(headTitle, headContent);
  121. }
  122. string xx = cookies.GetCookieHeader(new Uri(url));
  123. using (Stream st = req.GetRequestStream())
  124. {
  125. st.Write(bContent.ToArray(), 0, bContent.Length);
  126. }
  127. HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
  128. Stream resSt = myResponse.GetResponseStream();
  129. StreamReader sr = new StreamReader(resSt, Encoding.UTF8);
  130. string xxx = sr.ReadToEnd();
  131. sr.Close();
  132. return xxx;
  133. }
  134. catch (Exception ee)
  135. {
  136. string msg = ee.Message + Environment.NewLine + Environment.NewLine;
  137. msg += "URL:" + url + Environment.NewLine + "Method:POST" + Environment.NewLine +
  138. "Content:" + Content + Environment.NewLine;
  139. System.Net.WebException webEE = ee as System.Net.WebException;
  140. if (webEE != null && webEE.Response != null)
  141. {
  142. using (System.IO.Stream st = webEE.Response.GetResponseStream())
  143. {
  144. using (System.IO.StreamReader sr = new System.IO.StreamReader(st))
  145. {
  146. string webmsg = sr.ReadToEnd();
  147. msg += Environment.NewLine + Environment.NewLine + webmsg;
  148. }
  149. }
  150. }
  151. throw new Exception(msg);
  152. }
  153. }
  154. /// <summary>
  155. /// https
  156. /// </summary>
  157. /// <param name="sender"></param>
  158. /// <param name="certificate"></param>
  159. /// <param name="chain"></param>
  160. /// <param name="errors"></param>
  161. /// <returns></returns>
  162. public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  163. {
  164. return true;
  165. }
  166. /// <summary>
  167. /// get
  168. /// </summary>
  169. /// <param name="url"></param>
  170. /// <param name="timeout"></param>
  171. /// <param name="headTitle"></param>
  172. /// <param name="headContent"></param>
  173. /// <returns></returns>
  174. public string HTTP_GET(string url, int timeout, string headTitle, string headContent)
  175. {
  176. //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
  177. //ServicePointManager.ServerCertificateValidationCallback =
  178. // new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  179. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  180. req.Method = "GET";
  181. req.ContentLength = 0;
  182. //req.ContentType = "application/json";
  183. req.ContentType = "application/x-www-form-urlencoded";
  184. req.CookieContainer = cookies;
  185. if (headTitle.Length > 0)
  186. {
  187. req.Headers.Add(headTitle, headContent);
  188. }
  189. req.Timeout = timeout * 1000;
  190. HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
  191. Stream resSt = myResponse.GetResponseStream();
  192. StreamReader sr = new StreamReader(resSt);
  193. string xxx = sr.ReadToEnd();
  194. sr.Close();
  195. return xxx;
  196. }
  197. /// <summary>
  198. /// delete
  199. /// </summary>
  200. /// <param name="url"></param>
  201. /// <returns></returns>
  202. public string HTTPSDelete(string url)
  203. {
  204. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
  205. ServicePointManager.ServerCertificateValidationCallback =
  206. new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  207. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  208. req.Method = "DELETE";
  209. req.ContentLength = 0;
  210. req.CookieContainer = cookies;
  211. string xxx = string.Empty;
  212. using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)
  213. {
  214. if (response.StatusCode == HttpStatusCode.OK)
  215. {
  216. HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
  217. Stream resSt = myResponse.GetResponseStream();
  218. StreamReader sr = new StreamReader(resSt);
  219. xxx = sr.ReadToEnd();
  220. sr.Close();
  221. }
  222. }
  223. return xxx;
  224. }
  225. }
  226. }