【问题标题】:Get Html source of current page in C# Windows Forms App在 C# Windows Forms App 中获取当前页面的 Html 源代码
【发布时间】:2011-12-11 22:35:25
【问题描述】:

我正在使用 BandOjects 和 C# Windows 窗体应用程序创建 Internet Explorer 插件,并且正在测试解析 HTML 源代码。我目前一直在根据站点的 URL 解析信息。

我想获取我拥有的使用登录名的示例网站的当前页面的 HTML 源代码。如果我使用我所在页面的 URL,它将始终获取登录页面的来源而不是实际页面,因为我的应用程序无法识别我已登录。我是否需要存储我的登录凭据使用某种 api 的网站?或者有没有办法抓取 HTML 的当前页面?我更喜欢后者,因为它似乎不会那么麻烦。谢谢!

【问题讨论】:

    标签: c# internet-explorer html-parsing


    【解决方案1】:

    我在我的一个应用程序中使用了这种方法:

    private static string RetrieveData(string url)
        {
    
            // used to build entire input
            var sb = new StringBuilder();
    
            // used on each read operation
            var buf = new byte[8192];
            try
            {
                // prepare the web page we will be asking for
                var request = (HttpWebRequest)
                                         WebRequest.Create(url);
    
               /* Using the proxy class to access the site
                * Uri proxyURI = new Uri("http://proxy.com:80");
                request.Proxy = new WebProxy(proxyURI);
                request.Proxy.Credentials = new NetworkCredential("proxyuser", "proxypassword");*/
    
                // execute the request
                var response = (HttpWebResponse)
                                           request.GetResponse();
    
                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();
    
                string tempString = null;
                int count = 0;
    
                do
                {
                    // fill the buffer with data
                    count = resStream.Read(buf, 0, buf.Length);
    
                    // make sure we read some data
                    if (count != 0)
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);
    
                        // continue building the string
                        sb.Append(tempString);
                    }
                } while (count > 0); // any more data to read?
    
            }
            catch(Exception exception)
            {
                MessageBox.Show(@"Failed to retrieve data from the network. Please check you internet connection: " +
                                exception);
            }
            return sb.ToString();
        }
    

    您只需传递需要检索代码的网页的 url。

    例如:

    string htmlSourceGoggle = RetrieveData("www.google.com") 
    

    注意:如果您使用代理访问互联网,您可以取消注释代理配置。将代理地址、用户名和密码替换为您使用的。

    用于通过代码登录。检查这个:Login to website, via C#

    【讨论】:

    • 非常感谢,这确实适用于基于 URL 获取源(我最初确实有工作)。但同样因为我的网站需要登录才能查看特定页面(例如,在查询字符串中有一个 id 指定它是什么页面的页面),它总是检索登录页面的来源,因为如果你试图去到那个页面只是在 url 上没有登录,它不会让你。不知道该怎么做,或者是否有什么我能做的。
    • 仍在使用链接中的示例以使其对我有用,但这基本上就是我想要的。谢谢!
    猜你喜欢
    • 2018-01-05
    • 2015-04-30
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2012-07-25
    • 2010-11-24
    相关资源
    最近更新 更多