123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- 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);
- }
- }
- }
- }
|