客户端无法下载网站源代码c#

客户端无法下载网站源代码c#,c#,html,web-scraping,webclient,C#,Html,Web Scraping,Webclient,我正在尝试下载一个网站的源代码,只要我按下按钮开始检索,程序就会无限期地卡住 我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpre

我正在尝试下载一个网站的源代码,只要我按下按钮开始检索,程序就会无限期地卡住

我的代码:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (WebClient client = new WebClient())
            {
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
                string html = client.DownloadString("https://www.finanzen.net/termine/unternehmen/");
                MessageBox.Show(html);
            }
        }
    }
}

如何解决此问题?感谢您的帮助:)

我更愿意使用这些方法的异步实现。请参见下面的示例:

private async void button1_Click(object sender, EventArgs e)
        {
            using (WebClient client = new WebClient())
            {
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
                string html = await client.DownloadStringAsync("https://www.finanzen.net/termine/unternehmen/");
                MessageBox.Show(html);
            }
        }
编辑:
我建议您在GUI中添加DataGrid,而不是在MessageBox中显示结果。结果将存储在一个列表中,然后显示在GUI上。

您对可怜的
消息框要求太多了。Show
html
的长度为
423467
chars。请尝试以下方法:

MessageBox.Show(html.Substring(0, Math.Min(html.Length, 1000)));

我们不回答我们“更喜欢”的问题。这也不会编译,因为事件处理程序必须返回
void
Task
无效)您不能等待
DownloadStringAsync
,该方法是事件驱动的,它不会返回可等待的任务。异步、可等待的版本是
DownloadStringTaskAsync
。您可以在Visual Studio中设置断点,查看导致此行为的原因。
WebClient
已经过时,您应该改用
HttpClient