MyHttpHelper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. namespace LYFZ.WinAPI
  8. {
  9. public class MyHttpHelper
  10. {
  11. /// <summary>
  12. /// 获取网址HTML
  13. /// </summary>
  14. /// <param name="URL">网址 </param>
  15. /// <returns> </returns>
  16. public static string GetHtml(string URL)
  17. {
  18. WebRequest wrt;
  19. wrt = WebRequest.Create(URL);
  20. wrt.Credentials = CredentialCache.DefaultCredentials;
  21. WebResponse wrp;
  22. wrp = wrt.GetResponse();
  23. string reader = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
  24. try
  25. {
  26. wrt.GetResponse().Close();
  27. }
  28. catch (WebException ex)
  29. {
  30. throw ex;
  31. }
  32. return reader;
  33. }
  34. /// <summary>
  35. /// 获取网站cookie
  36. /// </summary>
  37. /// <param name="URL">网址 </param>
  38. /// <param name="cookie">cookie </param>
  39. /// <returns> </returns>
  40. public static string GetHtml(string URL, out string cookie)
  41. {
  42. WebRequest wrt;
  43. wrt = WebRequest.Create(URL);
  44. wrt.Credentials = CredentialCache.DefaultCredentials;
  45. WebResponse wrp;
  46. wrp = wrt.GetResponse();
  47. string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
  48. try
  49. {
  50. wrt.GetResponse().Close();
  51. }
  52. catch (WebException ex)
  53. {
  54. throw ex;
  55. }
  56. cookie = wrp.Headers.Get("Set-Cookie");
  57. return html;
  58. }
  59. public static string GetHtml(string URL, string postData, string cookie, out string header, string server)
  60. {
  61. return GetHtml(server, URL, postData, cookie, out header);
  62. }
  63. public static string GetHtml(string server, string URL, string postData, string cookie, out string header)
  64. {
  65. byte[] byteRequest = Encoding.GetEncoding("gb2312").GetBytes(postData);
  66. return GetHtml(server, URL, byteRequest, cookie, out header);
  67. }
  68. public static string GetHtml(string server, string URL, byte[] byteRequest, string cookie, out string header)
  69. {
  70. byte[] bytes = GetHtmlByBytes(server, URL, byteRequest, cookie, out header);
  71. Stream getStream = new MemoryStream(bytes);
  72. StreamReader streamReader = new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
  73. string getString = streamReader.ReadToEnd();
  74. streamReader.Close();
  75. getStream.Close();
  76. return getString;
  77. }
  78. /// <summary>
  79. /// Post模式浏览
  80. /// </summary>
  81. /// <param name="server">服务器地址 </param>
  82. /// <param name="URL">网址 </param>
  83. /// <param name="byteRequest">流 </param>
  84. /// <param name="cookie">cookie </param>
  85. /// <param name="header">句柄 </param>
  86. /// <returns> </returns>
  87. public static byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie, out string header)
  88. {
  89. long contentLength;
  90. HttpWebRequest httpWebRequest;
  91. HttpWebResponse webResponse;
  92. Stream getStream;
  93. httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
  94. CookieContainer co = new CookieContainer();
  95. co.SetCookies(new Uri(server), cookie);
  96. httpWebRequest.CookieContainer = co;
  97. httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  98. httpWebRequest.Accept =
  99. "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
  100. httpWebRequest.Referer = server;
  101. httpWebRequest.UserAgent =
  102. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
  103. httpWebRequest.Method = "Post";
  104. httpWebRequest.ContentLength = byteRequest.Length;
  105. Stream stream;
  106. stream = httpWebRequest.GetRequestStream();
  107. stream.Write(byteRequest, 0, byteRequest.Length);
  108. stream.Close();
  109. webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  110. header = webResponse.Headers.ToString();
  111. getStream = webResponse.GetResponseStream();
  112. contentLength = webResponse.ContentLength;
  113. byte[] outBytes = new byte[contentLength];
  114. outBytes = ReadFully(getStream);
  115. getStream.Close();
  116. return outBytes;
  117. }
  118. public static byte[] ReadFully(Stream stream)
  119. {
  120. byte[] buffer = new byte[128];
  121. using (MemoryStream ms = new MemoryStream())
  122. {
  123. while (true)
  124. {
  125. int read = stream.Read(buffer, 0, buffer.Length);
  126. if (read <= 0)
  127. return ms.ToArray();
  128. ms.Write(buffer, 0, read);
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Get模式
  134. /// </summary>
  135. /// <param name="URL">网址 </param>
  136. /// <param name="cookie">cookies </param>
  137. /// <param name="header">句柄 </param>
  138. /// <param name="server">服务器 </param>
  139. /// <param name="val">服务器 </param>
  140. /// <returns> </returns>
  141. public static string GetHtml(string URL, string cookie, out string header, string server)
  142. {
  143. return GetHtml(URL, cookie, out header, server, "");
  144. }
  145. /// <summary>
  146. /// Get模式浏览
  147. /// </summary>
  148. /// <param name="URL">Get网址 </param>
  149. /// <param name="cookie">cookie </param>
  150. /// <param name="header">句柄 </param>
  151. /// <param name="server">服务器地址 </param>
  152. /// <param name="val"> </param>
  153. /// <returns> </returns>
  154. public static string GetHtml(string URL, string cookie, out string header, string server, string val)
  155. {
  156. HttpWebRequest httpWebRequest;
  157. HttpWebResponse webResponse;
  158. Stream getStream;
  159. StreamReader streamReader;
  160. string getString = "";
  161. httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
  162. httpWebRequest.Accept = "*/*";
  163. httpWebRequest.Referer = server;
  164. CookieContainer co = new CookieContainer();
  165. co.SetCookies(new Uri(server), cookie);
  166. httpWebRequest.CookieContainer = co;
  167. httpWebRequest.UserAgent =
  168. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
  169. httpWebRequest.Method = "GET";
  170. webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  171. header = webResponse.Headers.ToString();
  172. getStream = webResponse.GetResponseStream();
  173. streamReader = new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
  174. getString = streamReader.ReadToEnd();
  175. streamReader.Close();
  176. getStream.Close();
  177. return getString;
  178. }
  179. }
  180. }