Get the contents of a web page from Firefox in a C # program

I need to write a simple C # application that should get all the contents of a webpage currently open in Firefox. Is there any way to do this directly from C #? If not, is it possible to develop some kind of plugin that will transfer the contents of the page? Since I am new to programming Firefox plugins, I would really appreciate any information on how to get me started quickly. Maybe there are some sources that I can use as a reference? Doc Links? Recommendations?

UPD : I really need to communicate with the Firefox instance, and not get the contents of the webpage from the given URL

+3
source share
3 answers

This will help if you clarify what you are trying to achieve. Maybe plugins already there, such as firebug, can help.

In any case, if you really want to develop both a plugin and a C # application:

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

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

+1
source

I think you will probably need to write a plugin for Firefox. However, of course, there are ways to request a webpage and get its HTML response in C #. Does it depend on your requirements?

, -, , .

0
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 ();

0
source

Source: https://habr.com/ru/post/1781819/


All Articles