我有一个正在运行的HttpWebRequest
,几周后,它突然停止工作并开始引发错误:
这是我的代码:
基础连接已关闭:发生意外错误
发送
HttpWebRequest FirstRequest = null;
HttpWebRequest postRequest = null;
HttpWebResponse response = null;
try
{
FirstRequest = (HttpWebRequest)WebRequest.Create("https://my.emerchantpay.com");
FirstRequest.CookieContainer = new CookieContainer();
FirstRequest.CookieContainer = _cookies;
FirstRequest.Accept = @"text/html, application/xhtml+xml, */*";
FirstRequest.Referer = @"https://my.emerchantpay.com";
FirstRequest.Headers.Add("Accept-Language", "en-GB");
FirstRequest.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";
FirstRequest.Host = @"my.emerchantpay.com";
response = (HttpWebResponse)FirstRequest.GetResponse();//here is error
}catch{}
我究竟做错了什么?
最佳答案
经过长期的研究,我找到了答案。
您需要添加ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
FirstRequest = (HttpWebRequest)WebRequest.Create("https://my.emerchantpay.com");
FirstRequest.CookieContainer = new CookieContainer();
FirstRequest.CookieContainer = _cookies;
FirstRequest.Accept = @"text/html, application/xhtml+xml, */*";
FirstRequest.Referer = @"https://my.emerchantpay.com";
FirstRequest.Headers.Add("Accept-Language", "en-GB");
FirstRequest.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";
FirstRequest.Host = @"my.emerchantpay.com";
response = (HttpWebResponse)FirstRequest.GetResponse();