gpt4 book ai didi

c# - 在 C# 中使用 Webclient 时如何忽略 SSL 检查

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:05 25 4
gpt4 key购买 nike

我正在尝试以字符串形式获取此 URL 的内容。

https://noembed.com/embed?url=https://www.youtube.com/watch?v=1FLhOGOg2Qg

这是我使用的代码:

        var html_content = "";

using (var client = new WebClient())

{


client.Headers.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36");

html_content += client.DownloadString("https://noembed.com/embed?url=https://www.youtube.com/watch?v=1FLhOGOg2Qg");


}
Console.WriteLine(html_content);
Console.ReadLine();

这是我得到的错误:

System.Net.WebException was unhandled
HResult=-2146233079
Message=The request was aborted: Could not create SSL/TLS secure channel.
Source=System

我在 WPF 应用程序上使用它,我可以在这里忽略 SSL。我已经尝试过其他忽略 SSL 的答案,但都没有用。它适用于其他网址,例如 https://www.youtube.com/watch?v=1FLhOGOg2Qg但不能使用 noembed.com URL。

最佳答案

添加 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;这对我有用:

var html_content = "";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

using (var client = new WebClient())

{


client.Headers.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36");

html_content += client.DownloadString("https://noembed.com/embed?url=https://www.youtube.com/watch?v=1FLhOGOg2Qg");


}
Console.WriteLine(html_content);
Console.ReadLine();

我得到的输出:

{"author_url":"https://www.youtube.com/user/nogoodflix ","url":"https://www.youtube.com/watch?v=1FLhOGOg2Qg ","provider_url":"https://www.youtube.com/ ","title":"ONE FOR THE MONEY 预告片 2011 官方 [高清] 凯瑟琳Heigl","author_name":"Streaming Clips","type":"video","height":270,"thumbnail_height":360,"thumbnail_width":480,"provider_name":"YouTube","html": "\nhttps://www.youtube.com/embed/1FLhOGOg2Qg?feature=oembed\"frameborder=\"0\"allowfullscreen=\"allowfullscreen\">\n","thumbnail_url":"https://i.ytimg.com/vi/1FLhOGOg2Qg/hqdefault.jpg ","version":"1.0","width":480}

关于c# - 在 C# 中使用 Webclient 时如何忽略 SSL 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47672959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com