Search code examples
c#htmlweb-scrapingwebclient

Client stuck downloading website source c#


I am trying to download a websites source code, as soon as I press the button to start retrieving, the programm would stuck infinitely

My code:


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);
            }
        }
    }
}

How can I fix this issue ? Any help appreciated :)


Solution

  • You are asking too much from the poor MessageBox.Show. The html is 423,467 chars long. Try this instead:

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