ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
string imageUrl = "https://www.barkerbrettell.co.uk/content/email-assets/BBLogo.png";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(imageUrl);
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
上述代码允许报错: Could not create SSL/TLS secure channel.
哪位大神有什么变通的方法吗?谢谢
操作系统是windows 7
(SecurityProtocolType)12288
試試這個?
try { //try TLS 1.3
ServicePointManager.SecurityProtocol = (SecurityProtocolType)12288
| (SecurityProtocolType)3072
| (SecurityProtocolType)768
| SecurityProtocolType.Tls;
} catch (NotSupportedException) {
try { //try TLS 1.2
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072
| (SecurityProtocolType)768
| SecurityProtocolType.Tls;
} catch (NotSupportedException) {
try { //try TLS 1.1
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768
| SecurityProtocolType.Tls;
} catch (NotSupportedException) { //TLS 1.0
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
}
}
}
试过了,报错了,可能需要机器上安装更高版本的framework吧
只能支持到1.2
@会长:
https://stackoverflow.com/questions/62677922/is-there-a-way-for-windows-7-to-support-tls-1-3-net-4-8
說是實驗性功能,目前只支援Win 10?
@RosonJ: 我想试试能不能通过其它进程把文件下载下来,然后在framework4平台上的程序去磁碟上读取。
@会长:
文件一定要透過TLS 1.3?
@RosonJ: 就是这个文件:https://www.barkerbrettell.co.uk/content/email-assets/BBLogo.png 这个文件是客户提供的,自己没法改变
@会长:
我抓包看是走TLS 1.2阿
@RosonJ: 你执行我开头贴的那段代码会报错吗,我是f12看的,等下我截图
@RosonJ:
@RosonJ: 是不是抓包工具比较老。我用Fiddler看,也显示1.2
@会长:
透過F12看確實是TLS 1.3
抓包卻是TLS 1.2,真奇特
@会长:
你這麼說道是有可能,我試試更新看看
@会长:
確實,升級之後顯示TLS 1.3
你手邊有Win 10環境嗎?把功能放到Win 10上試試?
@RosonJ: 好,我试试,不过就算可以也没太大用处,也不能让用户重新安装操作系统
@RosonJ: 试了下同事的windows 10,确实是可以
@会长:
只能跟用戶說Win 7 EOL了...
@RosonJ: 我试了用python下载,毫无压力
import os
os.makedirs('./image/', exist_ok=True)
IMAGE_URL = "https://www.barkerbrettell.co.uk/content/email-assets/BBLogo.png"
def urllib_download():
from urllib.request import urlretrieve
urlretrieve(IMAGE_URL, './image/img1.png')
def request_download():
import requests
r = requests.get(IMAGE_URL)
with open('./image/img2.png', 'wb') as f:
f.write(r.content)
def chunk_download():
import requests
r = requests.get(IMAGE_URL, stream=True)
with open('./image/img3.png', 'wb') as f:
for chunk in r.iter_content(chunk_size=32):
f.write(chunk)
if __name__ == '__main__':
urllib_download()
print('download img1')
request_download()
print('download img2')
chunk_download()
print('download img3')
@RosonJ: 我最近搞996,没空来答题了,我的园豆要被你超越了....
@会长:
怎麼搞起996了?
我的園豆還差你一大截呢哈哈
@RosonJ: 活多人少,利润低
@会长:
真慘啊...
我写了一段python代码,然后把它用pyinstaller搞成exe文件,在C#代码里调用.....
import os
import sys
from urllib.request import urlretrieve
import logging
import time
if __name__ == '__main__':
today = time.strftime("%Y%m%d",time.localtime(time.time()))
logdir = 'logs'
if not os.path.exists(logdir):
os.mkdir(logdir)
logging.basicConfig(filename= logdir + '\\' + today + '.log', level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
try:
url = sys.argv[1]
path = sys.argv[2]
urlretrieve(url, path)
except BaseException as e:
logging.error(e)