using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace BLL.Common { public class HttpHelper { /// /// Http Get Request /// /// /// public static string HttpGetRequest(string url, string token ="") { string strGetResponse = string.Empty; try { var getRequest = CreateHttpRequest(url,"GET", token); 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 bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //总是接受 } public static string PostJsonByHttps(string url, string jsonParams) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; HttpWebRequest request = null; CookieContainer cookie = new CookieContainer(); //HTTPSQ请求 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = cookie; request.ProtocolVersion = HttpVersion.Version11; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; byte[] byteData = Encoding.UTF8.GetBytes(jsonParams); int length = byteData.Length; request.ContentLength = length; using (Stream stream = request.GetRequestStream()) { stream.Write(byteData, 0, byteData.Length); } var response = (HttpWebResponse)request.GetResponse(); using (StreamReader st = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"))) { return st.ReadToEnd().ToString(); } } 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,token); } if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase)) { request = CreatePostHttpWebRequest(url, strJson[0].ToString(),token); } return request; } private static HttpWebRequest CreateGetHttpWebRequest(string url,string token) { var getRequest = HttpWebRequest.Create(url) as HttpWebRequest; if (!string.IsNullOrEmpty(token)) { getRequest.ContentType = "application/json"; getRequest.Headers.Add("token", token); } else { getRequest.ContentType = "text/html;charset=UTF-8"; } getRequest.Method = "GET"; getRequest.Timeout = 5000; 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; } } }