How can i download a website content to a string?

后端 未结 3 1672
庸人自扰
庸人自扰 2020-12-21 10:51

I tried this and i want that the source content of the website will be download to a string:

public partial class Form1 : Form
    {
        WebClient client         


        
3条回答
  •  误落风尘
    2020-12-21 10:59

    If you are using .Net 4.5,

    public async void Downloader()
    {
        using (WebClient wc = new WebClient())
        {
            string page = await wc.DownloadStringTaskAsync("http://chatroll.com/rotternet");
        }
    }
    

    For 3.5 or 4.0

    public void Downloader()
    {
        using (WebClient wc = new WebClient())
        {
            wc.DownloadStringCompleted += (s, e) =>
            {
                string page = e.Result;
            };
            wc.DownloadStringAsync(new Uri("http://chatroll.com/rotternet"));
        }
    }
    

提交回复
热议问题