HttpUtility.cs 1.1 KB

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