Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET Web会话中的WebBrowser控件_C#_Asp.net_Webbrowser Control - Fatal编程技术网

C# ASP.NET Web会话中的WebBrowser控件

C# ASP.NET Web会话中的WebBrowser控件,c#,asp.net,webbrowser-control,C#,Asp.net,Webbrowser Control,情景: 我想使用WebBrowser控件在研究项目的外部网站上代理网站导航。因此,我尝试使用WebBrowser控件在页面请求中加载站点,并转发接收到的HTML,并进行一些修改(如src/href和javascript事件处理程序aso所更改)。当参与者/用户在代理网站上触发onclick事件时,我会在服务器上获取此事件,并希望在WebBrowser控件内重新触发它 问题: 我不知道如何处理WebBrowser控件。起初我认为这只是将其存储为会话对象的问题,但它必须在STA线程中运行这一事实使这

情景: 我想使用WebBrowser控件在研究项目的外部网站上代理网站导航。因此,我尝试使用WebBrowser控件在页面请求中加载站点,并转发接收到的HTML,并进行一些修改(如src/href和javascript事件处理程序aso所更改)。当参与者/用户在代理网站上触发onclick事件时,我会在服务器上获取此事件,并希望在WebBrowser控件内重新触发它

问题: 我不知道如何处理WebBrowser控件。起初我认为这只是将其存储为会话对象的问题,但它必须在STA线程中运行这一事实使这一点变得困难。当用户调用onclick事件以允许我在控件上代理此onclick时,我需要相同的、活动的浏览器对象。 现在,我使用一个包装类IEBrowser:System.Windows.Forms.ApplicationContext。 我从不同的来源复制代码,主要是从(),但它不考虑使用相同的WebBub控件对许多请求。 以下是IEBrowser类中的一些代码:

public void Nav(string url)
{
    this.url = url;
    this.resultEvent = new AutoResetEvent(false);
    htmlResult = null;
    ths = new ThreadStart(delegate
    {
        // create a WebBrowser control
        ieBrowser = new WebBrowser();
        //Reset Session
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
        // set WebBrowser event handls
        ieBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(IEBrowser_DocumentCompleted);
        //make request
        ieBrowser.Navigate(url);

        System.Windows.Forms.Application.Run(this);
        //remove WebBrowser event handler
        ieBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(IEBrowser_DocumentIsCompleted);
        //for now, we keep the webBrowser open
        //ieBrowser.Dispose();
    });
    thrd = new Thread(ths);
    thrd.Name = "Thread 2";
    thrd.IsBackground = true;
    // set thread to STA state before starting
    thrd.SetApartmentState(ApartmentState.STA);
    thrd.Start();
    EventWaitHandle.WaitAll(new AutoResetEvent[] { resultEvent });
    thrd.Join();
}

// DocumentCompleted event handle
void IEBrowser_DocumentIsCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (ieBrowser.ReadyState == WebBrowserReadyState.Complete && ieBrowser.IsBusy == false)
    {
        //Replace or Set IDs on every HTML Element [...]
        //...
        //
        ieBrowser.Stop();
        ExitThread();
        //Dispose();
        resultEvent.Set();
    }
}
限制:
这不是关于性能,我需要做这个远程,但只有1-5人将同时使用该网站。我知道使用WebBrowser控件通常可能不是一个好的解决方案,但在本例中,它正是捕获所有用户导航所需要的

您是否试图在服务器进程中使用WebBrowser?@SimonMourier是。有没有办法保存浏览器并在下一次回发时重新加载?可能是序列化或保存会话和浏览器数据?我不知道这是否适合您的操作,但根据页面的不同,您是否能够调用页面中的脚本来重新触发事件?如果是这样,您可以使用类似ieBrowser.Document.InvokeScript(“triggerEvent”);其中“triggerEvent”是要调用以复制事件的函数名。@RobWilkins不需要这样做。我需要让浏览器保持活动状态,以便对其进行解析,在网页上向用户显示详细信息,允许他们做出选择,然后返回浏览器控件并调用这些操作。所以必须在回发之间保持它的活力。