using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Text; using System.Threading; namespace ProductionLineMonitor.Core.Utils { public class HttpHelper { public static bool Get(string url, string inputData, ref string outData, int timeOut = 10) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "application/json"; request.Timeout = timeOut * 1000; try { if (inputData == null) { using Stream stream = request.GetRequestStream(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream revStream = response.GetResponseStream(); using StreamReader reader = new StreamReader(revStream); outData = reader.ReadToEnd(); return true; } else { byte[] data = Encoding.UTF8.GetBytes(inputData); request.ContentLength = data.Length; using Stream stream = request.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream revStream = response.GetResponseStream(); using StreamReader reader = new StreamReader(revStream); outData = reader.ReadToEnd(); return true; } } catch (Exception ex) { outData = ex.Message; return false; } } public static bool Post(string url, string inputData, ref string outData, int timeOut = 10) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json"; request.Timeout = timeOut * 1000; try { if (inputData == null) { using (Stream stream = request.GetRequestStream()) { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream revStream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(revStream)) { outData = reader.ReadToEnd(); return true; } } } else { byte[] data = Encoding.UTF8.GetBytes(inputData); request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream revStream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(revStream)) { outData = reader.ReadToEnd(); return true; } } } } catch (Exception ex) { outData = ex.Message; return false; } } public static (bool, string) Get(string url, string inputData, int timeOut = 10) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "application/json"; request.Timeout = timeOut * 1000; try { if (inputData == null) { using Stream stream = request.GetRequestStream(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream revStream = response.GetResponseStream(); using StreamReader reader = new StreamReader(revStream); return (true, reader.ReadToEnd()); } else { byte[] data = Encoding.UTF8.GetBytes(inputData); request.ContentLength = data.Length; using Stream stream = request.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream revStream = response.GetResponseStream(); using StreamReader reader = new StreamReader(revStream); return (true, reader.ReadToEnd()); } } catch (Exception ex) { return (false, ex.Message); } } public static (bool, string) Post(string url, string inputData, int timeOut = 10) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json"; request.Timeout = timeOut * 1000; try { if (inputData == null) { using (Stream stream = request.GetRequestStream()) { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream revStream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(revStream)) { return (true, reader.ReadToEnd()); } } } else { byte[] data = Encoding.UTF8.GetBytes(inputData); request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream revStream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(revStream)) { return (true, reader.ReadToEnd()); } } } } catch (Exception ex) { return (false, ex.Message); } } } }