Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET web browser control sources using asp.net

Tags:

c#

asp.net

vb.net

How do I get the source of another web site using the WebBrowser() control in an asp.net code behind page.

so far I have this but dont have any options to navagate or the set cookies or to get the page loaded source? can you please help?

Imports System.Windows.Forms


Partial Class _Default
    Inherits System.Web.UI.Page

    Dim a As WebBrowser = New WebBrowser()
    webBrowser1.Navigate("http://www.google.com")
    'get the source

End Class

And I get this error

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

like image 990
Hello-World Avatar asked May 18 '26 10:05

Hello-World


2 Answers

this will work perfectly

we need to add webbrowser with threading other wise we will get ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. error

this is the way we can make webbrowser work in asp.net webpages

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Windows.Forms;

/// <summary>
/// Summary description for CustomBrowser
/// </summary>
public class CustomBrowser
{
    public CustomBrowser()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    protected string _url;
    string html = "";
    public string GetWebpage(string url)
    {
        _url = url;
        // WebBrowser is an ActiveX control that must be run in a
        // single-threaded apartment so create a thread to create the
        // control and generate the thumbnail
        Thread thread = new Thread(new ThreadStart(GetWebPageWorker));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();
        string s = html;
        return s;
    }

    protected void GetWebPageWorker()
    {
        using (WebBrowser browser = new WebBrowser())
        {
            //  browser.ClientSize = new Size(_width, _height);
            browser.ScrollBarsEnabled = false;
            browser.ScriptErrorsSuppressed = true;
            browser.Navigate(_url);

            // Wait for control to load page
            while (browser.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();

            html = browser.DocumentText;

        }
    }

}

in webpage

CustomBrowser browser = new CustomBrowser();
string s = browser.GetWebpage("http://localhost:8781/WebSite3/Default3.aspx");
Response.Write(s);
like image 199
kavali rakesh Avatar answered May 21 '26 01:05

kavali rakesh


What you're looking to do is slightly unorthodox in ASP.NET, but here is an example that may help you:

http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET

like image 36
KP. Avatar answered May 21 '26 00:05

KP.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!