using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace BLL.Common { public class HttpHelper { /// /// Http Get Request /// /// /// public static string HttpGetRequest(string url) { string strGetResponse = string.Empty; try { var getRequest = CreateHttpRequest(url,"GET", ""); var getResponse = getRequest.GetResponse() as HttpWebResponse; strGetResponse = GetHttpResponse(getResponse, "GET"); } catch (Exception ex) { strGetResponse = ex.Message; } return strGetResponse; } /// /// Http Post Request /// /// /// /// public static string HttpPostRequest(string url, string postJsonData,string token) { BLL.ErrLogInfo.WriteLog("token=" + token); string strPostReponse = string.Empty; try { var postRequest = CreateHttpRequest(url, "POST",token, postJsonData); var postResponse = postRequest.GetResponse() as HttpWebResponse; strPostReponse = GetHttpResponse(postResponse, "POST"); BLL.ErrLogInfo.WriteLog("result=" + strPostReponse); } catch (Exception ex) { strPostReponse = ex.Message; BLL.ErrLogInfo.WriteLog("error=" + ex.Message); } return strPostReponse; } private static HttpWebRequest CreateHttpRequest(string url, string requestType, string token, params object[] strJson) { HttpWebRequest request = null; const string get = "GET"; const string post = "POST"; if (string.Equals(requestType, get, StringComparison.OrdinalIgnoreCase)) { request = CreateGetHttpWebRequest(url); } if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase)) { request = CreatePostHttpWebRequest(url, strJson[0].ToString(),token); } return request; } private static HttpWebRequest CreateGetHttpWebRequest(string url) { var getRequest = HttpWebRequest.Create(url) as HttpWebRequest; getRequest.Method = "GET"; getRequest.Timeout = 5000; getRequest.ContentType = "text/html;charset=UTF-8"; getRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; return getRequest; } private static HttpWebRequest CreatePostHttpWebRequest(string url, string postData,string token) { var postRequest = HttpWebRequest.Create(url) as HttpWebRequest; postRequest.KeepAlive = false; postRequest.Method = "POST"; postRequest.ContentType = "application/x-www-form-urlencoded"; if (!string.IsNullOrEmpty(token)) { postRequest.ContentType = "application/json"; postRequest.Headers.Add("token_app", token); } postRequest.ContentLength = postData.Length; postRequest.AllowWriteStreamBuffering = false; StreamWriter writer = new StreamWriter(postRequest.GetRequestStream(), Encoding.ASCII); writer.Write(postData); writer.Flush(); return postRequest; } private static string GetHttpResponse(HttpWebResponse response, string requestType) { var responseResult = ""; const string post = "POST"; string encoding = "UTF-8"; if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase)) { encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) { encoding = "UTF-8"; } } using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding))) { responseResult = reader.ReadToEnd(); } return responseResult; } } }