Requester.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Rac.Models;
  2. using System.Net;
  3. using System;
  4. namespace Rac.Tools
  5. {
  6. // This class can change to instance class
  7. // Implement different driver (WebRequest/WinNet/Sock/Etc(Pr0xy))
  8. internal static class Requester
  9. {
  10. public static Response GetHttp(string url, string referer = null)
  11. {
  12. var req = WebRequest.CreateHttp(url);
  13. req.UserAgent = "RawArchiveCrawler";
  14. if (referer != null) req.Referer = new Uri(referer).AbsoluteUri;
  15. req.AllowAutoRedirect = false; //record redirections
  16. try
  17. {
  18. var resp = (HttpWebResponse)req.GetResponse();
  19. return new Response
  20. {
  21. StatusCode = resp.StatusCode,
  22. StatusDescription = resp.StatusDescription,
  23. Headers = resp.Headers,
  24. Body = resp.GetResponseStream().ToBytes()
  25. };
  26. }
  27. catch (WebException e)
  28. {
  29. if (e.Status != WebExceptionStatus.ProtocolError) throw;
  30. var resp = (HttpWebResponse)e.Response;
  31. return new Response
  32. {
  33. StatusCode = resp.StatusCode,
  34. StatusDescription = resp.StatusDescription,
  35. Headers = resp.Headers,
  36. Body = resp.GetResponseStream().ToBytes()
  37. };
  38. }
  39. }
  40. }
  41. }