public class HttpHelper { /// <summary> /// 证书校验 /// </summary> /// <param name="sender"></param> /// <param name="certificate"></param> /// <param name="chain"></param> /// <param name="errors"></param> /// <returns></returns> //public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) //{ // // 总是接受 // return true; //} /// <summary> /// HttpGet /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <returns></returns> public static T HttpGet<T>(string url) { Stream stream = null; StreamReader sr = null; try { WebRequest ret = WebRequest.Create(url); ret.Method = "Get"; ret.ContentType = "application/json;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)ret.GetResponse(); stream = ret.GetResponse().GetResponseStream(); sr = new StreamReader(stream, Encoding.UTF8); string ApiResult = sr.ReadToEnd(); var t = (T)JsonConvert.DeserializeObject<T>(ApiResult); return t; } catch { throw; } finally { stream.Close(); sr.Close(); } } /// <summary> /// HttpPost /// </summary> /// <typeparam name="T"></typeparam> /// <param name="paramData"></param> /// <param name="postUrl"></param> /// <param name="Token"></param> /// <returns></returns> public static T HttpPost<T>(string paramData, string postUrl, string Token = "") { StreamReader sr = null; Stream stream = null; HttpWebResponse response = null; try { byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化 //ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证 HttpWebRequest ret = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); if (!string.IsNullOrEmpty(Token)) { ret.Headers["token"] = Token; } ret.Method = "POST"; ret.ContentType = "application/json"; ret.ContentLength = byteArray.Length; stream = ret.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length);//写入参数 stream.Close(); try { response = (HttpWebResponse)ret.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; } sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string ApiResult = sr.ReadToEnd(); if (response.StatusCode != HttpStatusCode.OK) { throw new WebException(ApiResult); } var t = JsonConvert.DeserializeObject<T>(ApiResult); return t; } catch (Exception ex) { throw new Exception(ex.Message); } finally { sr.Close(); response.Close(); stream.Close(); } } public static T HttpBytePost<T>(byte[] byteArray, string postUrl) { StreamReader sr = null; Stream stream = null; HttpWebResponse response = null; try { //byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化 HttpWebRequest ret = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); ret.Method = "POST"; ret.ContentType = "application/octet-stream"; ret.ContentLength = byteArray.Length; stream = ret.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length);//写入参数 stream.Close(); response = (HttpWebResponse)ret.GetResponse(); sr = new StreamReader(response.GetResponseStream(), Encoding.Default); string ApiResult = sr.ReadToEnd(); var t = JsonConvert.DeserializeObject<T>(ApiResult); return t; } catch (Exception ex) { throw ex; } finally { sr.Close(); response.Close(); stream.Close(); } }