How can i download a website content to a string?

后端 未结 3 1673
庸人自扰
庸人自扰 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 11:12

    Using WebRequest:

    WebRequest request = WebRequest.Create(url);
    request.Method = "GET";
    WebResponse response = request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream);
    string content = reader.ReadToEnd();
    reader.Close();
    response.Close();
    

    You can easily call the code from within another thread, or use background worer - that will make your UI responsive while retrieving data.

提交回复
热议问题