【问题标题】:How can I ignore SSL checks while using Webclient in C#在 C# 中使用 Webclient 时如何忽略 SSL 检查
【发布时间】:2017-12-06 11:12:30
【问题描述】:

我正在尝试将此 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 网址。

【问题讨论】:

    标签: c# wpf ssl


    【解决方案1】:

    添加 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 Trailer 2011 Official [HD] Katherine Heigl","author_name":"流媒体剪辑","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", "版本":"1.0","宽度":480}

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 2021-09-26
      • 1970-01-01
      • 2015-06-23
      • 2018-06-08
      • 2015-01-29
      • 2018-08-25
      • 2010-11-21
      相关资源
      最近更新 更多