本文介绍了我在C#中使用Web抓取和html敏捷包出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 private void button1_Click(object sender, EventArgs e)
    {
        var html = @"https://html-agility-pack.net/from-web";
        HtmlWeb web = new HtmlWeb();
        var htmldoc = web.Load(html);
        var nod = htmldoc.DocumentNode.SelectSingleNode("//head/title");
        textBox1.Text = "Node Name: " + nod.Name + "\n" + nod.OuterHtml;
    }

起初我想使用c#和html敏捷包学习网络爬虫,但是当我运行我的第一个代码时,将出现以下消息:

at first i want learning webscraoing with c# and html agility pack but when I run my first code this meassage will appear:

其他信息:基础连接已关闭:An 发送时发生意外错误.}

Additional information: The underlying connection was closed: An unexpected error occurred on a send.}

推荐答案

基础连接已关闭:发送时发生意外错误

由于您尝试访问HTTPS页面,因此可能必须在创建请求之前设置安全协议.

Since you're trying to accessing HTTPS page you may had to set security protocol before creating request.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var html = @"https://html-agility-pack.net/from-web";
HtmlWeb web = new HtmlWeb();

这将首选TLS 1.2,但仍允许使用1.1和1.0(以防止失败,因为某些站点不提供TLS 1.2).

This will prefer TLS 1.2 but still allow 1.1 and 1.0 (to prevent failure because some sites doesn't offer TLS 1.2).

这篇关于我在C#中使用Web抓取和html敏捷包出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:03