新的 X509Store(StoreName.My, StoreLocation.CurrentUser) 没有任何证书
这个功能用于在服务器上发送数据,当服务器有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论