using NIU.Core; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace CP.Common { public class DoHttpHelp { public static string PostHttp(string url, string methodType, Dictionary headers, string body = null) { HttpWebRequest request = null; StreamWriter sw = null; StreamReader s = null; string ret; try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;//解决:请求被中止: 未能创建 SSL/TLS 安全通道 request = (HttpWebRequest)WebRequest.Create(url);//请求 request.Method = methodType; if (headers != null && headers.Count > 0) for (var i = 0; i < headers.Count; i++) request.Headers.Add(headers.ElementAt(i).Key, headers.ElementAt(i).Value); request.KeepAlive = true; //request.CookieContainer = cook; request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36"; request.ServicePoint.Expect100Continue = false; request.ServicePoint.ConnectionLimit = int.MaxValue; if (!string.IsNullOrEmpty(body)) { sw = new StreamWriter(request.GetRequestStream());//获取写入流 sw.Write(body); sw.Flush(); //强制写入 } WebResponse response = request.GetResponse(); //获取响应 s = new StreamReader(response.GetResponseStream());//获取响应流 ret = s.ReadToEnd(); //读取数据 } catch (Exception ex) { throw ex; } finally { if (s != null) s.Close(); if (sw != null) sw.Close(); if (request != null) request.Abort(); s = null; sw = null; request = null; } return ret; } /// /// 清理webapi开奖号数据缓存 /// /// /// public static bool UpdateWebApi(string ename) { string apiUrl = ConfigurationManager.AppSettings["api55128"].ToString(); string wwwUrl = ConfigurationManager.AppSettings["www55128"].ToString(); apiUrl += ename; wwwUrl += ename; PostHttp(wwwUrl, "Get", null); PostHttp(apiUrl, "Get", null); return true; } /// /// 清理webapi开奖号数据缓存 /// /// /// public static bool UpdateWebApi_admin(string ename) { string apiUrl = ConfigurationManager.AppSettings["api55128"].ToString(); string wwwUrl = ConfigurationManager.AppSettings["www55128"].ToString(); apiUrl += ename; wwwUrl += ename; var z = PostHttp(wwwUrl, "Get", null); var a = PostHttp(apiUrl, "Get", null); if (z == "0" && a.Contains("0")) return true; else throw new OperationExceptionFacade("清除缓存失败"); } public static string PostHttpForAdmin(string url, string methodType, Dictionary headers, string body = null) { HttpWebRequest request = null; StreamWriter sw = null; StreamReader s = null; string ret; try { request = (HttpWebRequest)WebRequest.Create(url);//请求 request.Method = methodType; if (headers != null && headers.Count > 0) for (var i = 0; i < headers.Count; i++) request.Headers.Add(headers.ElementAt(i).Key, headers.ElementAt(i).Value); request.KeepAlive = true; //request.CookieContainer = cook; request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"; request.ServicePoint.Expect100Continue = false; request.ServicePoint.ConnectionLimit = int.MaxValue; if (!string.IsNullOrEmpty(body)) { byte[] bytes = Encoding.Default.GetBytes(body); Stream sendStream = request.GetRequestStream(); sendStream.Write(bytes, 0, bytes.Length); sendStream.Close(); } WebResponse response = request.GetResponse(); //获取响应 s = new StreamReader(response.GetResponseStream());//获取响应流 ret = s.ReadToEnd(); //读取数据 } catch (Exception ex) { throw ex; } finally { if (s != null) s.Close(); if (sw != null) sw.Close(); if (request != null) request.Abort(); s = null; sw = null; request = null; } return ret; } } }