HttpUtility.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. namespace Muchinfo.MTPClient.Infrastructure.Utilities
  5. {
  6. public enum RequestMethod
  7. {
  8. Get, Post
  9. }
  10. public class HttpUtility
  11. {
  12. public static string HttpGet(string url)
  13. {
  14. var request = CreateGetRequest(url);
  15. try
  16. {
  17. var response = (HttpWebResponse)request.GetResponse();
  18. var stream = response.GetResponseStream();
  19. if (stream == null) return string.Empty;
  20. var streamReader = new StreamReader(stream);
  21. string responseContent = streamReader.ReadToEnd();
  22. response.Close();
  23. streamReader.Close();
  24. return responseContent;
  25. }
  26. catch (Exception e)
  27. {
  28. Console.WriteLine(e.ToString());
  29. return null;
  30. }
  31. }
  32. private static HttpWebRequest CreateGetRequest(string url)
  33. {
  34. url = url.Trim();
  35. if (!url.Contains("://"))
  36. url = "http://" + url;
  37. var request = (HttpWebRequest)WebRequest.Create(url);
  38. request.ContentType = "application/json";
  39. request.Method = "GET";
  40. request.Timeout = 20000;
  41. return request;
  42. }
  43. }
  44. }