MywebClient.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. namespace Gets
  7. {
  8. public class MyWebClient : WebClient
  9. {
  10. public CookieContainer m_container = new CookieContainer();
  11. string refrer = string.Empty;
  12. public string ReferURL
  13. {
  14. get
  15. {
  16. return this.refrer;
  17. }
  18. set
  19. {
  20. this.refrer = value;
  21. }
  22. }
  23. protected override WebRequest GetWebRequest(Uri address)
  24. {
  25. WebRequest webRequest = base.GetWebRequest(address);
  26. if (webRequest is HttpWebRequest)
  27. {
  28. (webRequest as HttpWebRequest).CookieContainer = this.m_container;
  29. if (ReferURL != string.Empty)
  30. {
  31. (webRequest as HttpWebRequest).Referer = ReferURL;
  32. }
  33. (webRequest as HttpWebRequest).ContentType = "application/x-www-form-urlencoded";
  34. (webRequest as HttpWebRequest).ContentType = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
  35. (webRequest as HttpWebRequest).UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
  36. }
  37. return webRequest;
  38. }
  39. public string GetHTML(Uri uri)
  40. {
  41. try
  42. {
  43. string kk = Encoding.UTF8.GetString(this.DownloadData(uri));
  44. return kk;
  45. }
  46. catch (Exception e)
  47. {
  48. throw e;
  49. }
  50. }
  51. }
  52. }