惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
Cloudbric
Cloudbric
V
Visual Studio Blog
L
LangChain Blog
A
About on SuperTechFans
B
Blog
T
Tenable Blog
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
博客园 - 三生石上(FineUI控件)
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Palo Alto Networks Blog
U
Unit 42
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
C
Check Point Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence
Application and Cybersecurity Blog
Application and Cybersecurity Blog
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
S
Securelist
Security Archives - TechRepublic
Security Archives - TechRepublic
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Latest news
Latest news
GbyAI
GbyAI
T
Troy Hunt's Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
V2EX - 技术
V2EX - 技术
小众软件
小众软件
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
Netflix TechBlog - Medium
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed

博客园 - 取经路上

IIS 下发布SignalR 访问接口进不去处理 Linux下部署.Net 应用程序和Web应用程序 CentOS 7 配置启动 手动编译的 nginx CentOS 7 nginx 安装 sticky模块 关于SignalR并发量测试 C# 调用迅雷aplayer播放器的遇到的问题总结 C# 判别系统版本以及Win10的识别办法 MVC ActionResult 视图模型 MVC 前后台传值 MVC基础关键点 sqlserver 数据库、日志文件收缩 笔记 vue-cli启动报错问题: IE6无法获取class属性 windows server 2008 r2 datacenter 共享服务找不到网络路径解决办法 在Visual Studio 中的监视窗口中监视Com对象变量 删除GitHub上项目中的某个文件 转 WPF MVVM 循序渐进 (从基础到高级) 服务器未能识别 HTTP 头 SOAPAction 的值: http://tempuri.org/QueryUserName。
C# 采用HttpWebRequest 、WebClient和HttpClient下载https的文件异常问题
取经路上 · 2024-02-22 · via 博客园 - 取经路上

今天有个客户反应,程序下载文件失败,无法正常使用。

远程客户电脑后,查看错误日志,都是提示https:****************************.dll等一系列文件的下载错误提示

提示基础连接已经关闭: 发送时发生错误。

在网上找了很多方案都没有能解决,大多都是https链接时增加指定协议,很遗憾未能解决

        HttpWebRequest request;
            HttpWebResponse response;
            request = (HttpWebRequest)WebRequest.Create(strUrl);
            if (isHttps)
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request.ProtocolVersion = HttpVersion.Version10;

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            }

找不到原因,客户又着急使用,就想着换成WebClient下载类试试,于是写了个粗糙的demo

WebClient webClient = new WebClient();
webClient.DownloadFile(textBox1.Text, Path.GetFileName(textBox1.Text));

发现还是提示同样的错误,这个时候一头雾水,心里万马奔腾

心思不能乱,不行我在换,72般武艺都耍上,于是用HttpClient类,写个demo试试

 using (HttpClient client = new HttpClient())
 {
     // 使用异步方法下载文件
     HttpResponseMessage response = await client.GetAsync(textBox1.Text);
     response.EnsureSuccessStatusCode(); // 确保请求成功

     // 保存文件
     using (FileStream fileStream = File.Create( Path.GetFileName(textBox1.Text)))
     {
         await response.Content.CopyToAsync(fileStream);
     }
 }

发现还是不能下载文件,但是错误提示有变化,提示为“因为算法不同,客户端与服务器无法通信

于是寻找这个错误的解决方案,需要在注册表这个位置将协议值修改下

计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols

 一看客户电脑上,好几个,一个个修改太麻烦,直接将协议改名,让这个设置失效。

在启动测试程序下载文件,发现正常了,完工。

后来在网上又发现一个解决方案,感觉可能能行,由于客户问题处理完后,无法在霸占其电脑了,将这个方案记录下,后续遇到在验证,或者有遇到相同问题的天涯沦落人验证后给我留言,谢谢

在工作中要获取一个网络api下的内容,因为要auth认证,本以为很简单

string url="https://.....";
string usernamePassword = CustomerID + ":" + CustomerCertificate;
string basic = Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword));
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Headers.Add("Authorization", "Basic " + basic);
HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();  //<-- 这里就报错了
Stream resStream = myRes.GetResponseStream();
StreamReader strReader = new StreamReader(resStream);
string resStr = strReader.ReadToEnd();
Console.WriteLine(resStr);

结果运行时提示“提示基础连接已经关闭: 发送时发生错误。”
在浏览器下访问网址则正常,刚开始怀疑是basic不对,在浏览器下用同样的值模拟了一下能正常获取,然后把https换成http程序又能正常获取了,定位到问题出在ssl证书上
查了一下资料
添加以下代码

ServicePointManager.Expect100Continue = true;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

改回https还是提示原来的异常,换了另外一个https的网址测试却又正常,在浏览器下对比两个https网址的ssl证书,发现能正常访问的测试网址是tls1.0 不能访问的网址是tls1.2
找到问题了,原来在.net4.5以下tls1.2的设置如下

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; 
//.net4.5及以上
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

再次运行,ok