C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点

C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点,第1张

概述HttpUtil工具类: using System; using System.Collections.Generic; using System.IO; using System.Linq; usi

httpUtil工具类:

using System; System.Collections.Generic; System.IO; System.linq; System.Net; System.Text;namespace Mybrowser{    /// <summary>    /// http上传下载文件    </summary>    public class httpUtil    {        #region @R_419_4601@        private static cookieContainer m_cookie = new cookieContainer();        static voID Sethttpcookie(cookieContainer cookie)        {            m_cookie = cookie;        }        #endregion        #region httpDownloadfile 下载文件        static MemoryStream httpDownloadfile(string url)        {            // 设置参数            httpWebRequest request = WebRequest.Create(url) as httpWebRequest;            request.Method = "GET";            request.cookieContainer = m_cookie;            发送请求并获取相应回应数据            httpWebResponse response = request.GetResponse()  httpWebResponse;            直到request.GetResponse()程序才开始向目标网页发送Post请求            Stream responseStream = response.GetResponseStream();            创建写入流            MemoryStream stream =  MemoryStream();            byte[] barr = new byte[1024];            int size = responseStream.Read(barr,0,(int)barr.Length);            while (size > 0)            {                stream.Write(barr,,size);                size = responseStream.Read(barr,1)">)barr.Length);            }            stream.Seek(return stream;        }        #region httpUploadfile 上传文件        <summary>         http上传文件        </summary>        string httpUploadfile(string url,byte[] barr,1)"> filename)        {             httpWebRequest;            cookieContainer cookieContainer =  cookieContainer();            request.cookieContainer = cookieContainer;            request.AllowautoRedirect = true;            request.Method = POST;            string boundary = DateTime.Now.Ticks.ToString(X");  随机分隔线            request.ContentType = text/plain;charset=utf-8 m_cookie;            Stream poststream = request.GetRequestStream();            poststream.Write(barr,barr.Length);            poststream.Close();            直到request.GetResponse()程序才开始向目标网页发送Post请求            Stream instream = response.GetResponseStream();            StreamReader sr =  StreamReader(instream,EnCoding.UTF8);            返回结果网页(HTML)代码            string content = sr.ReadToEnd();             content;        }        #region httpPost         httpPost        string httpPost( data)        {            byte[] barr = ASCIIEnCoding.UTF8.GetBytes(data);             httpWebRequest;            request.cookieContainer = m_cookie;            request.Method = ;            request.ContentType = ;            request.ContentLength = barr.Length;            Stream poststream =;            #region httpGet         httpGet        string httpGet(out cookie cookie)        {             sr.ReadToEnd();            获取cookie            cookieCollection cc = request.cookieContainer.Getcookies(request.RequestUri);            if (cc != null && cc.Count > )            {                cookie = cc[];            }            else            {                cookie = null;            }            #endregion    }}
VIEw Code

 C#检测并安装https站点数字证书

bool isCertStored = ;X509Store store =  X509Store(Storename.Root,StoreLocation.LocalMachine);store.Open(OpenFlags.MaxAllowed);X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectname,1)">HIKVISION-GA-ROOT-V1",1)">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 + );        store.Add(certificate); 安装证书    }        {        isCertStored = ;    }}store.Close();if (!isCertStored){    System.windows.MessageBox.Show(证书没有安装成功,无法访问系统,请联系管理员);    ;}
VIEw Code

 CefSharp和httpWebRequest通过会话cookie实现自动登录:

voID Window_Loaded(object sender,RoutedEventArgs e){    Task.Factory.StartNew(() =>    {        try        {            获取token            string tokenUrl = tokenUrl = _baseUrl + /nopasswordLogin_A10AD3FA1EC4fdbAA9B7007BBF3FCD89/?ACTION=getTokenstring 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;            httpUtil.httpGet(loginUrl, cookie);            if (cookie != )            {                CefSharp.cookie cefsharpcookie =  CefSharp.cookie();                cefsharpcookie.Domain = cookie.Domain;                cefsharpcookie.name = cookie.name;                cefsharpcookie.Value = cookie.Value;                this.dispatcher.BeginInvoke(new Action(() =>                {                                        {                        _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();                    }                }));            }                        {                System.windows.MessageBox.Show(登录失败,请联系管理员);                .Close();            }        }                {            System.windows.MessageBox.Show();            .Close();        }    });}
VIEw Code

 

总结

以上是内存溢出为你收集整理的C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点全部内容,希望文章能够帮你解决C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1212665.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存