C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点
程序员文章站
2022-07-04 19:42:11
HttpUtil工具类: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; namespace MyBrow ......
HttpUtil工具类:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; namespace MyBrowser { /// <summary> /// Http上传下载文件 /// </summary> public class HttpUtil { #region cookie设置 private static CookieContainer m_Cookie = new CookieContainer(); public static void SetHttpCookie(CookieContainer cookie) { m_Cookie = cookie; } #endregion #region HttpDownloadFile 下载文件 public static MemoryStream HttpDownloadFile(string url) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.CookieContainer = m_Cookie; //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); //创建写入流 MemoryStream stream = new MemoryStream(); byte[] bArr = new byte[1024]; int size = responseStream.Read(bArr, 0, (int)bArr.Length); while (size > 0) { stream.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, (int)bArr.Length); } stream.Seek(0, SeekOrigin.Begin); responseStream.Close(); return stream; } #endregion #region HttpUploadFile 上传文件 /// <summary> /// Http上传文件 /// </summary> public static string HttpUploadFile(string url, byte[] bArr, string fileName) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线 request.ContentType = "text/plain;charset=utf-8"; request.CookieContainer = m_Cookie; Stream postStream = request.GetRequestStream(); postStream.Write(bArr, 0, bArr.Length); postStream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } #endregion #region HttpPost /// <summary> /// HttpPost /// </summary> public static string HttpPost(string url, string data) { byte[] bArr = ASCIIEncoding.UTF8.GetBytes(data); // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = m_Cookie; request.Method = "POST"; request.ContentType = "text/plain;charset=utf-8"; request.ContentLength = bArr.Length; Stream postStream = request.GetRequestStream(); postStream.Write(bArr, 0, bArr.Length); postStream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } #endregion #region HttpPost /// <summary> /// HttpPost /// </summary> public static string HttpPost(string url) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = m_Cookie; request.Method = "POST"; request.ContentType = "text/plain;charset=utf-8"; //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } #endregion #region HttpGet /// <summary> /// HttpGet /// </summary> public static string HttpGet(string url) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = m_Cookie; request.Method = "GET"; request.ContentType = "text/plain;charset=utf-8"; //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } #endregion #region HttpGet /// <summary> /// HttpGet /// </summary> public static string HttpGet(string url, out Cookie cookie) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = m_Cookie; request.Method = "GET"; request.ContentType = "text/plain;charset=utf-8"; //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); //获取Cookie CookieCollection cc = request.CookieContainer.GetCookies(request.RequestUri); if (cc != null && cc.Count > 0) { cookie = cc[0]; } else { cookie = null; } return content; } #endregion } }
C#检测并安装https站点的数字证书:
bool isCertStored = true; X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine); store.Open(OpenFlags.MaxAllowed); X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "HIKVISION-GA-ROOT-V1", false); if (certs.Count == 0 || certs[0].NotAfter < DateTime.Now) { if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "HuShaCert.cer")) { X509Certificate2 certificate = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + "HuShaCert.cer"); store.Add(certificate); //安装证书 } else { isCertStored = false; } } store.Close(); if (!isCertStored) { System.Windows.MessageBox.Show("证书没有安装成功,无法访问系统,请联系管理员"); return; }
CefSharp和HttpWebRequest通过会话Cookie实现自动登录:
private void Window_Loaded(object sender, RoutedEventArgs e) { Task.Factory.StartNew(() => { try { //获取token string tokenUrl = tokenUrl = _baseUrl + "/noPasswordLogin_A10AD3FA1EC4FDBAA9B7007BBF3FCD89/?ACTION=getToken"; string token = HttpUtil.HttpGet(tokenUrl); if (token != null) token = token.Trim(); //登录,并取得Cookie string loginUrl = _baseUrl + string.Format("/noPasswordLogin_A10AD3FA1EC4FDBAA9B7007BBF3FCD89/?ACTION=verify&VERIFY_TOKEN={0}&YISA_API_NAME=yisa_api1", token); System.Net.Cookie cookie = null; HttpUtil.HttpGet(loginUrl, out cookie); if (cookie != null) { CefSharp.Cookie cefsharpCookie = new CefSharp.Cookie(); cefsharpCookie.Domain = cookie.Domain; cefsharpCookie.Name = cookie.Name; cefsharpCookie.Value = cookie.Value; this.Dispatcher.BeginInvoke(new Action(() => { try { _browser = new ExtChromiumBrowser() { Dock = DockStyle.Fill }; BindBrowser(_browser); //设置Cookie var cookieManager = CefSharp.Cef.GetGlobalCookieManager(); cookieManager.SetCookieAsync(_baseUrl, cefsharpCookie); _browser.Load(_baseUrl + "/?c=inquiry_model"); host.Child = _browser; } catch { System.Windows.MessageBox.Show("无法访问,请联系管理员"); this.Close(); } })); } else { System.Windows.MessageBox.Show("登录失败,请联系管理员"); this.Close(); } } catch { System.Windows.MessageBox.Show("无法访问,请联系管理员"); this.Close(); } }); }
上一篇: iOS-项目创建多个target
下一篇: C# 实现http不同方法的请求