How I can get web page's content and save it into the string variable

前端 未结 4 1536
走了就别回头了
走了就别回头了 2020-11-28 03:59

How I can get the content of the web page using ASP.NET? I need to write a program to get the HTML of a webpage and store it into a string variable.

4条回答
  •  萌比男神i
    2020-11-28 04:23

    I've run into issues with Webclient.Downloadstring before. If you do, you can try this:

    WebRequest request = WebRequest.Create("http://www.google.com");
    WebResponse response = request.GetResponse();
    Stream data = response.GetResponseStream();
    string html = String.Empty;
    using (StreamReader sr = new StreamReader(data))
    {
        html = sr.ReadToEnd();
    }
    

提交回复
热议问题