HttpHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading;
  8. namespace ProductionLineMonitor.Core.Utils
  9. {
  10. public class HttpHelper
  11. {
  12. public static bool Get(string url, string inputData, ref string outData, int timeOut = 10)
  13. {
  14. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  15. request.Method = "GET";
  16. request.ContentType = "application/json";
  17. request.Timeout = timeOut * 1000;
  18. try
  19. {
  20. if (inputData == null)
  21. {
  22. using Stream stream = request.GetRequestStream();
  23. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  24. Stream revStream = response.GetResponseStream();
  25. using StreamReader reader = new StreamReader(revStream);
  26. outData = reader.ReadToEnd();
  27. return true;
  28. }
  29. else
  30. {
  31. byte[] data = Encoding.UTF8.GetBytes(inputData);
  32. request.ContentLength = data.Length;
  33. using Stream stream = request.GetRequestStream();
  34. stream.Write(data, 0, data.Length);
  35. stream.Close();
  36. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  37. Stream revStream = response.GetResponseStream();
  38. using StreamReader reader = new StreamReader(revStream);
  39. outData = reader.ReadToEnd();
  40. return true;
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. outData = ex.Message;
  46. return false;
  47. }
  48. }
  49. public static bool Post(string url, string inputData, ref string outData, int timeOut = 10)
  50. {
  51. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  52. request.Method = "POST";
  53. request.ContentType = "application/json";
  54. request.Timeout = timeOut * 1000;
  55. try
  56. {
  57. if (inputData == null)
  58. {
  59. using (Stream stream = request.GetRequestStream())
  60. {
  61. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  62. Stream revStream = response.GetResponseStream();
  63. using (StreamReader reader = new StreamReader(revStream))
  64. {
  65. outData = reader.ReadToEnd();
  66. return true;
  67. }
  68. }
  69. }
  70. else
  71. {
  72. byte[] data = Encoding.UTF8.GetBytes(inputData);
  73. request.ContentLength = data.Length;
  74. using (Stream stream = request.GetRequestStream())
  75. {
  76. stream.Write(data, 0, data.Length);
  77. stream.Close();
  78. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  79. Stream revStream = response.GetResponseStream();
  80. using (StreamReader reader = new StreamReader(revStream))
  81. {
  82. outData = reader.ReadToEnd();
  83. return true;
  84. }
  85. }
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. outData = ex.Message;
  91. return false;
  92. }
  93. }
  94. public static (bool, string) Get(string url, string inputData, int timeOut = 10)
  95. {
  96. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  97. request.Method = "GET";
  98. request.ContentType = "application/json";
  99. request.Timeout = timeOut * 1000;
  100. try
  101. {
  102. if (inputData == null)
  103. {
  104. using Stream stream = request.GetRequestStream();
  105. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  106. Stream revStream = response.GetResponseStream();
  107. using StreamReader reader = new StreamReader(revStream);
  108. return (true, reader.ReadToEnd());
  109. }
  110. else
  111. {
  112. byte[] data = Encoding.UTF8.GetBytes(inputData);
  113. request.ContentLength = data.Length;
  114. using Stream stream = request.GetRequestStream();
  115. stream.Write(data, 0, data.Length);
  116. stream.Close();
  117. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  118. Stream revStream = response.GetResponseStream();
  119. using StreamReader reader = new StreamReader(revStream);
  120. return (true, reader.ReadToEnd());
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. return (false, ex.Message);
  126. }
  127. }
  128. public static (bool, string) Post(string url, string inputData, int timeOut = 10)
  129. {
  130. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  131. request.Method = "POST";
  132. request.ContentType = "application/json";
  133. request.Timeout = timeOut * 1000;
  134. try
  135. {
  136. if (inputData == null)
  137. {
  138. using (Stream stream = request.GetRequestStream())
  139. {
  140. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  141. Stream revStream = response.GetResponseStream();
  142. using (StreamReader reader = new StreamReader(revStream))
  143. {
  144. return (true, reader.ReadToEnd());
  145. }
  146. }
  147. }
  148. else
  149. {
  150. byte[] data = Encoding.UTF8.GetBytes(inputData);
  151. request.ContentLength = data.Length;
  152. using (Stream stream = request.GetRequestStream())
  153. {
  154. stream.Write(data, 0, data.Length);
  155. stream.Close();
  156. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  157. Stream revStream = response.GetResponseStream();
  158. using (StreamReader reader = new StreamReader(revStream))
  159. {
  160. return (true, reader.ReadToEnd());
  161. }
  162. }
  163. }
  164. }
  165. catch (Exception ex)
  166. {
  167. return (false, ex.Message);
  168. }
  169. }
  170. }
  171. }