使用WebClient获取网页HTML

时间:2019-10-08 07:32:57

标签: c# web-scraping

我正在尝试获取“ reject_callback”的源代码, 但是结果与Google Chrome中的inspect中的源代码不一样。

这是我的代码:

https://codal.ir

1 个答案:

答案 0 :(得分:1)

假设您只需要静态网页

的源代码
    private void Button2_Click(object sender, EventArgs e)
    {
        string urlAddress = "https://codal.ir";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = null;

                if (response.CharacterSet == null)
                {
                    readStream = new StreamReader(receiveStream);
                }
                else
                {
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                }

                string data = readStream.ReadToEnd(); // Data variable here
                readStream.Close();
            }
        }
    }