首页 新闻 会员 周边 捐助

如何在Framework4环境下下载使用了TLS1.3的网站中的图片

0
悬赏园豆:50 [已解决问题] 解决于 2021-04-20 20:42
            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

会长的主页 会长 | 专家六级 | 园豆:12463
提问于:2021-04-20 15:53
< >
分享
最佳答案
0

(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;
        }
    }
}
收获园豆:50
RosonJ | 老鸟四级 |园豆:4910 | 2021-04-20 16:20

试过了,报错了,可能需要机器上安装更高版本的framework吧

会长 | 园豆:12463 (专家六级) | 2021-04-20 16:34

只能支持到1.2

会长 | 园豆:12463 (专家六级) | 2021-04-20 16:39
RosonJ | 园豆:4910 (老鸟四级) | 2021-04-20 16:47

@RosonJ: 我想试试能不能通过其它进程把文件下载下来,然后在framework4平台上的程序去磁碟上读取。

会长 | 园豆:12463 (专家六级) | 2021-04-20 17:14

@会长:
文件一定要透過TLS 1.3?

RosonJ | 园豆:4910 (老鸟四级) | 2021-04-20 17:16

@RosonJ: 就是这个文件:https://www.barkerbrettell.co.uk/content/email-assets/BBLogo.png 这个文件是客户提供的,自己没法改变

会长 | 园豆:12463 (专家六级) | 2021-04-20 17:19

@会长:
我抓包看是走TLS 1.2阿

RosonJ | 园豆:4910 (老鸟四级) | 2021-04-20 17:21

@RosonJ: 你执行我开头贴的那段代码会报错吗,我是f12看的,等下我截图

会长 | 园豆:12463 (专家六级) | 2021-04-20 17:27

@RosonJ:

会长 | 园豆:12463 (专家六级) | 2021-04-20 17:28

@RosonJ: 是不是抓包工具比较老。我用Fiddler看,也显示1.2

会长 | 园豆:12463 (专家六级) | 2021-04-20 17:42

@会长:
透過F12看確實是TLS 1.3
抓包卻是TLS 1.2,真奇特

RosonJ | 园豆:4910 (老鸟四级) | 2021-04-20 17:42

@会长:
你這麼說道是有可能,我試試更新看看

RosonJ | 园豆:4910 (老鸟四级) | 2021-04-20 17:44

@会长:
確實,升級之後顯示TLS 1.3
你手邊有Win 10環境嗎?把功能放到Win 10上試試?

RosonJ | 园豆:4910 (老鸟四级) | 2021-04-20 17:47

@RosonJ: 好,我试试,不过就算可以也没太大用处,也不能让用户重新安装操作系统

会长 | 园豆:12463 (专家六级) | 2021-04-20 17:59

@RosonJ: 试了下同事的windows 10,确实是可以

会长 | 园豆:12463 (专家六级) | 2021-04-20 18:00

@会长:
只能跟用戶說Win 7 EOL了...

RosonJ | 园豆:4910 (老鸟四级) | 2021-04-20 18:02

@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')
会长 | 园豆:12463 (专家六级) | 2021-04-20 18:30

@RosonJ: 我最近搞996,没空来答题了,我的园豆要被你超越了....

会长 | 园豆:12463 (专家六级) | 2021-04-20 22:00

@会长:
怎麼搞起996了?
我的園豆還差你一大截呢哈哈

RosonJ | 园豆:4910 (老鸟四级) | 2021-04-21 08:56

@RosonJ: 活多人少,利润低

会长 | 园豆:12463 (专家六级) | 2021-04-21 09:22

@会长:
真慘啊...

RosonJ | 园豆:4910 (老鸟四级) | 2021-04-21 09:34
其他回答(1)
0

我写了一段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) 
会长 | 园豆:12463 (专家六级) | 2021-04-20 20:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册