GetToolPlus.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Azylee.YeahWeb.HttpUtils.MethodUtils.ExtendUtils;
  2. using Azylee.YeahWeb.HttpUtils.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. namespace Azylee.YeahWeb.HttpUtils.MethodUtils.GetUtils
  10. {
  11. internal static class GetToolPlus
  12. {
  13. internal static string Get(string url, ref CookieCollection cookie, Dictionary<string, string> headers = null, string contentType = HttpContentTypes.ApplicationXWwwFormUrlEncoded, bool autoRedirect = false, bool keepAlive = true, string userAgent = UserAgents.Mozilla4)
  14. {
  15. string html = "";
  16. Stream stream = null;
  17. StreamReader reader = null;
  18. try
  19. {
  20. HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  21. request.Method = "GET";
  22. request.ContentType = contentType;
  23. request.AllowAutoRedirect = autoRedirect;
  24. request.KeepAlive = keepAlive;
  25. request.UserAgent = userAgent;
  26. request.CookieContainer = new CookieContainer();
  27. HeaderTool.Set(ref request, headers);
  28. if (cookie != null) request.CookieContainer.Add(cookie);
  29. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  30. response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
  31. cookie = response.Cookies;
  32. stream = response.GetResponseStream();
  33. reader = new StreamReader(stream, Encoding.Default);
  34. html = reader.ReadToEnd();
  35. }
  36. catch
  37. {
  38. }
  39. finally
  40. {
  41. if (reader != null) reader.Close();
  42. if (stream != null) stream.Close();
  43. }
  44. return html;
  45. }
  46. }
  47. }