客户端卡住下载网站源c#

Client stuck downloading website source c#

我正在尝试下载一个网站的源代码,我一按下按钮开始检索,程序就卡死了

我的代码:


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 上。

你对穷人的要求太高了MessageBox.Showhtml423,467 个字符长。试试这个:

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