当前位置:
首页 > Python基础教程 >
-
C#教程之C#检测并安装https站点的数字证书,CefS
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(); } }); }
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式