using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; namespace ICSSoft.Frame.Data.DAL { public class WebHelper { public static T HttpPost(string url, string body, string tocken = null) { T t; try { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*"; request.ContentType = "application/json; charset=utf-8"; if (tocken != null) request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + tocken); byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { t = JsonConvert.DeserializeObject(reader.ReadToEnd()); } return t; } catch (WebException webEx) { if (webEx.Response != null) { using (var errorResponse = (HttpWebResponse)webEx.Response) { using (Stream errorDataStream = errorResponse.GetResponseStream()) { StreamReader reader = new StreamReader(errorDataStream); string errorResponseContent = reader.ReadToEnd(); try { t = JsonConvert.DeserializeObject(errorResponseContent); } catch (Exception ex) { throw new Exception(webEx.InnerException.Message); } return t; } } } else { throw new Exception(webEx.Message); } } } public static T HttpGet(string url, string tocken = null) { T t; try { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*"; request.ContentType = "application/json; charset=utf-8"; if (tocken != null) request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + tocken); var res = request.GetResponse(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { t = JsonConvert.DeserializeObject(reader.ReadToEnd()); } return t; } catch (WebException webEx) { if (webEx.Response != null) { using (var errorResponse = (HttpWebResponse)webEx.Response) { using (Stream errorDataStream = errorResponse.GetResponseStream()) { StreamReader reader = new StreamReader(errorDataStream); string errorResponseContent = reader.ReadToEnd(); try { t = JsonConvert.DeserializeObject(errorResponseContent); } catch (Exception ex) { throw new Exception(webEx.InnerException.Message); } return t; } } } else { throw new Exception(webEx.Message); } } } public static string GetIp() { IPAddress[] ipv4Addresses = Dns.GetHostAddresses(Dns.GetHostName()); string ip = ""; foreach (IPAddress ipv4Address in ipv4Addresses) { if (ipv4Address.AddressFamily == AddressFamily.InterNetwork) { ip = ipv4Address.ToString(); } } return ip; } } }