在 C# 程序中从 Firefox 获取网页内容

发布于 2024-10-08 21:58:01 字数 233 浏览 0 评论 0原文

我需要编写一个简单的 C# 应用程序,该应用程序应该接收当前在 Firefox 中打开的网页的全部内容。有没有办法直接从C#中做到这一点?如果没有,是否可以开发某种可以传输页面内容的插件?由于我是 Firefox 插件编程的新手,因此我非常感谢任何能让我快速入门的信息。也许有一些资源可以作为参考?文档链接?建议?

UPD:我实际上需要与 Firefox 实例通信,而不是从给定 URL 获取网页内容

I need to write a simple C# app that should receive entire contents of a web page currently opened in Firefox. Is there any way to do it directly from C#? If not, is it possible to develop some kind of plug-in that would transfer page contents? As I am a total newbie in Firefox plug-ins programming, I'd really appreciate any info on getting me started quickly. Maybe there are some sources I can use as a reference? Doc links? Recommendations?

UPD: I actually need to communicate with a Firefox instance, not get contents of a web page from a given URL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆伤 2024-10-15 21:58:01

如果您详细说明您想要实现的目标,将会有所帮助。可能已有的插件(例如 firebug)可以提供帮助。

无论如何,如果您确实想同时开发插件和 C# 应用程序:

请查看有关 Firefox 扩展的教程:
http://robertnyman.com/2009/01/ 24/how-to-develop-a-firefox-extension/

另外,您可以在.NET请求中使用WebRequest或HttpWebRequest类来获取任何URL的HTML源。

It would help if you elaborate What you are trying to achieve. May be plugins already out there such as firebug can help.

Anways, if you really want to develop both plugin and C# application:

Check out this tutorial on firefox extension:
http://robertnyman.com/2009/01/24/how-to-develop-a-firefox-extension/

Otherwise, You can use WebRequest or HttpWebRequest class in .NET request to get the HTML source of any URL.

潇烟暮雨 2024-10-15 21:58:01

我想你几乎肯定需要为此编写一个 Firefox 插件。然而,肯定有一些方法可以在 C# 中请求网页并接收其 HTML 响应。这取决于你的要求是什么?

如果您的要求只是从任何网站接收源代码,请发表评论,我会向您指出代码。

I think you'd almost certainly need to write a Firefox plugin for that. However there are certainly ways to request a webpage, and receive its HTML response within C#. It depends on what your requirements are?

If you're requirements are simply receive the source from any website, leave a comment and I'll point you towards the code.

暗恋未遂 2024-10-15 21:58:01

原生消息传递启用扩展程序与用户计算机上安装的本机应用程序交换消息。

Native messaging enables an extension to exchange messages with a native application installed on the user's computer.

浮生未歇 2024-10-15 21:58:01
Uri uri = new Uri(url); 
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri.AbsoluteUri);
        req.AllowAutoRedirect = true;
        req.MaximumAutomaticRedirections = 3;
        //req.UserAgent = _UserAgent; //"Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Searcharoo.NET)";
        req.KeepAlive = true;
        req.Timeout = _RequestTimeout * 1000; //prefRequestTimeout 

        // SIMONJONES http://codeproject.com/aspnet/spideroo.asp?msg=1421158#xx1421158xx
        req.CookieContainer = new System.Net.CookieContainer();
        req.CookieContainer.Add(_CookieContainer.GetCookies(uri));

System.Net.HttpWebResponse webresponse = null;

        try
        {
            webresponse = (System.Net.HttpWebResponse)req.GetResponse();
        }
        catch (Exception ex)
        {
            webresponse = null;
            Console.Write("request for url failed: {0} {1}", url, ex.Message);
        }

        if (webresponse != null)
        {
            webresponse.Cookies = req.CookieContainer.GetCookies(req.RequestUri);
            // handle cookies (need to do this incase we have any session cookies)
            foreach (System.Net.Cookie retCookie in webresponse.Cookies)
            {
                bool cookieFound = false;
                foreach (System.Net.Cookie oldCookie in _CookieContainer.GetCookies(uri))
                {
                    if (retCookie.Name.Equals(oldCookie.Name))
                    {
                        oldCookie.Value = retCookie.Value;
                        cookieFound = true;
                    }
                }
                if (!cookieFound)
                {
                    _CookieContainer.Add(retCookie);
                }
            }
string enc = "utf-8"; // default
            if (webresponse.ContentEncoding != String.Empty)
            {
                // Use the HttpHeader Content-Type in preference to the one set in META
                doc.Encoding = webresponse.ContentEncoding;
            }
            else if (doc.Encoding == String.Empty)
            {
                doc.Encoding = enc; // default
            }
            //http://www.c-sharpcorner.com/Code/2003/Dec/ReadingWebPageSources.asp
            System.IO.StreamReader stream = new System.IO.StreamReader
                (webresponse.GetResponseStream(), System.Text.Encoding.GetEncoding(doc.Encoding));

webresponse.Close();

Uri uri = new Uri(url); 
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri.AbsoluteUri);
        req.AllowAutoRedirect = true;
        req.MaximumAutomaticRedirections = 3;
        //req.UserAgent = _UserAgent; //"Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Searcharoo.NET)";
        req.KeepAlive = true;
        req.Timeout = _RequestTimeout * 1000; //prefRequestTimeout 

        // SIMONJONES http://codeproject.com/aspnet/spideroo.asp?msg=1421158#xx1421158xx
        req.CookieContainer = new System.Net.CookieContainer();
        req.CookieContainer.Add(_CookieContainer.GetCookies(uri));

System.Net.HttpWebResponse webresponse = null;

        try
        {
            webresponse = (System.Net.HttpWebResponse)req.GetResponse();
        }
        catch (Exception ex)
        {
            webresponse = null;
            Console.Write("request for url failed: {0} {1}", url, ex.Message);
        }

        if (webresponse != null)
        {
            webresponse.Cookies = req.CookieContainer.GetCookies(req.RequestUri);
            // handle cookies (need to do this incase we have any session cookies)
            foreach (System.Net.Cookie retCookie in webresponse.Cookies)
            {
                bool cookieFound = false;
                foreach (System.Net.Cookie oldCookie in _CookieContainer.GetCookies(uri))
                {
                    if (retCookie.Name.Equals(oldCookie.Name))
                    {
                        oldCookie.Value = retCookie.Value;
                        cookieFound = true;
                    }
                }
                if (!cookieFound)
                {
                    _CookieContainer.Add(retCookie);
                }
            }
string enc = "utf-8"; // default
            if (webresponse.ContentEncoding != String.Empty)
            {
                // Use the HttpHeader Content-Type in preference to the one set in META
                doc.Encoding = webresponse.ContentEncoding;
            }
            else if (doc.Encoding == String.Empty)
            {
                doc.Encoding = enc; // default
            }
            //http://www.c-sharpcorner.com/Code/2003/Dec/ReadingWebPageSources.asp
            System.IO.StreamReader stream = new System.IO.StreamReader
                (webresponse.GetResponseStream(), System.Text.Encoding.GetEncoding(doc.Encoding));

webresponse.Close();

神回复 2024-10-15 21:58:01

这就是你想要的。

using System.Net;

var cli = new WebClient();
string data = cli.DownloadString("http://www.heise.de");
Console.WriteLine(data);

This does what you want.

using System.Net;

var cli = new WebClient();
string data = cli.DownloadString("http://www.heise.de");
Console.WriteLine(data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文