当前位置:编程学习 > C#/ASP.NET >>

C# 获取浏览器页面内容

如题用C#怎么获取当前浏览器打开的所有页面的页面内容?浏览器不一定是IE 有可能是其他的浏览器,请问代码怎么写? --------------------编程问答-------------------- 其实呢,你可以通过输入网址来获取

利用HttpWebRequest 或者 WebClient --------------------编程问答-------------------- 使用HttpWebRequest和HttpWebResponse类就可以的 --------------------编程问答--------------------

private string GetWebContent(string sUrl)
        {
            string strResult = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                //声明一个HttpWebRequest请求
                request.Timeout = 3000000;
                //设置连接超时时间
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.ToString() != "")
                {
                    Stream streamReceive = response.GetResponseStream();
                    Encoding encoding = Encoding.GetEncoding("UTF-8");
                    StreamReader streamReader = new StreamReader(streamReceive, encoding);
                    strResult = streamReader.ReadToEnd();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            return strResult;
        }
--------------------编程问答-------------------- 这样也可以,但是也要获取浏览器打开网址啊,如果是IE就能获取了,但是如果用FF那些就获取不了了 --------------------编程问答-------------------- 用webbrowser控件,给它一个url地址,然后获取里面的内容即可。 --------------------编程问答-------------------- --------------------编程问答-------------------- 给个例子,看不懂啊。。。 --------------------编程问答-------------------- //读取httpUrl页面内容的类(返回整个页面内容html)
    public class Helpers
    {
        /// <summary>
        /// Gets the HTTP stream.
        /// </summary>
        /// <param name="pUrl">The p URL.</param>
        /// <param name="pTimeout">The p timeout.</param>
        /// <returns>A System.IO.Stream value...</returns>
        public Stream GetHttpStream(string pUrl, int pTimeout)
        {
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(pUrl); 
            wr.Timeout = pTimeout * 1000; // milliseconds to seconds 
            WebResponse resp = wr.GetResponse();
            return (resp.GetResponseStream());
        } 
    } --------------------编程问答-------------------- 又一个,哈哈,刚开始写采集的程序员伤不起啊! --------------------编程问答--------------------
引用 8 楼 congplayer 的回复:
//读取httpUrl页面内容的类(返回整个页面内容html)
  public class Helpers
  {
  /// <summary>
  /// Gets the HTTP stream.
  /// </summary>
  /// <param name="pUrl">The p URL.</param>
  /// <param name="pTimeout">……

用这个方法是可以,但是要获取当前浏览器所打开的网站地址啊,我现在只能获取IE浏览器的,其他非IE内核的浏览器获取不了,请问怎么获取?先谢谢各位了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,