新的 X509Store(StoreName.My, StoreLocation.CurrentUser) 没有任何证书

发布于 2024-09-19 16:00:22 字数 2535 浏览 4 评论 0原文

这个功能用于在服务器上发送数据,当服务器有 HTTP 协议时一切都很好,但是 HTTP 协议在 HTTPS 上更改了,我遇到了问题,我正在尝试更新我的方法,但我的商店(X509Store)没有证书。

这个方法应该怎么看呢?

static public List<object> SendRequestCook(string textRequest, string url, string referer, CookieContainer cooks, string metod, string proxy)
    {
        try
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));



            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

            X509Certificate2Collection certs =
              store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
            request.ClientCertificates.Add(certs[0]);






            System.Net.ServicePointManager.Expect100Continue = false;
            byte[] byteArray = Encoding.UTF8.GetBytes(textRequest);

            request.Method = "POST";
            request.ReadWriteTimeout = int.MaxValue;
            request.CookieContainer = cooks;
            //request.ProtocolVersion = HttpVersion.Version11;
            request.ContentLength = textRequest.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request.Headers.Add("Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7");
            request.UserAgent = "Mozilla/4.0";
            request.KeepAlive = true;
            request.Referer = referer;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteArray, 0, textRequest.Length);


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();


            Stream responseStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(responseStream, enc);
            List<object> list = new List<object>();
            list.Add(cooks);
            string currResponse = readStream.ReadToEnd();
            list.Add(currResponse);
            list.Add(metod);
            readStream.Close();
            response.Close();

            return list;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
            return SendRequestCook(textRequest, url, referer, cooks, metod, "");
        }
    }

This function for sending data on server, when server had HTTP protocol everything was good, but HTTP protocol was changed on HTTPS and I got a problem, I was trying update my method, but my store (X509Store) have no certificate.

How should look this method?

static public List<object> SendRequestCook(string textRequest, string url, string referer, CookieContainer cooks, string metod, string proxy)
    {
        try
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));



            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

            X509Certificate2Collection certs =
              store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
            request.ClientCertificates.Add(certs[0]);






            System.Net.ServicePointManager.Expect100Continue = false;
            byte[] byteArray = Encoding.UTF8.GetBytes(textRequest);

            request.Method = "POST";
            request.ReadWriteTimeout = int.MaxValue;
            request.CookieContainer = cooks;
            //request.ProtocolVersion = HttpVersion.Version11;
            request.ContentLength = textRequest.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request.Headers.Add("Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7");
            request.UserAgent = "Mozilla/4.0";
            request.KeepAlive = true;
            request.Referer = referer;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteArray, 0, textRequest.Length);


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();


            Stream responseStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(responseStream, enc);
            List<object> list = new List<object>();
            list.Add(cooks);
            string currResponse = readStream.ReadToEnd();
            list.Add(currResponse);
            list.Add(metod);
            readStream.Close();
            response.Close();

            return list;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
            return SendRequestCook(textRequest, url, referer, cooks, metod, "");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文