【问题标题】:c# Web browser keeps freezing but regular IE9 browser does notc# Web 浏览器一直冻结,但普通的 IE9 浏览器没有
【发布时间】:2013-08-26 01:14:43
【问题描述】:

我正在使用 Visual Studio C# 2010 作为 Web 浏览器。

WebBrowser 1 导航到此链接:

http://www.costco.com/IOGEAR-Wireless-1080p-HDMI-Transmitter-and-Receiver-3D-Compatible-2x-HDMI-Ports.product.100011675.html

当它到达页面时,它会加载并冻结。

我认为网页没有问题,因为 chrome、firefox 和常规 IE9 根本不会冻结。

只有我的 c# 程序中的网络浏览器在导航到此链接时会冻结。

如何防止它冻结?该网页似乎正在从另一个站点调用一些 html 数据。

我尝试将此代码添加到我的程序中

this.webBrowser1.ScriptErrorsSuppressed = true;

我还更改了 Web 浏览器的注册表值,以便它可以使用 Internet Explorer 版本 9,但到目前为止这两个都不起作用。

这是我正在使用的代码

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;

 namespace WindowsFormsApplication1
 {
     public partial class Form1 : Form
     {
         public Form1()
    {
        InitializeComponent();

        webBrowser1.ScriptErrorsSuppressed = true;
   }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate("http://www.costco.com/IOGEAR-Wireless-1080p-HDMI-Transmitter-and-Receiver-3D-Compatible-2x-HDMI-Ports.product.100011675.html");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

         }
     }
 }

【问题讨论】:

  • 您可以检查 WebBrowser 控件是否实际使用与您的 Internet Explorer 相同的 IE 版本。 (听起来很奇怪,但请耐心等待。)使用每个来访问可以检测浏览器版本的页面(例如,whatismybrowser.com)。可能是 webbrowser 控件使用的是旧版本的 IE ...您可以在注册表中进行更改。见这里:stackoverflow.com/questions/5531452/… 和这里:stackoverflow.com/questions/4612255/…
  • 我已经这样做了,我将 dword 更改为 9999,它表明我正在使用 Internet Explorer 9.. 您能否使用上面的代码在您的计算机中重新创建我的程序?我相信你会得到同样的结果

标签: c# browser


【解决方案1】:

问题不在于 WebBrowser 控件本身,而在于该特定网站如何尝试执行一些陷入循环的 Javascript。

比较和对比:

1) 将网址更改为http://google.com。工作正常。

2) 现在。为 Navigating 事件添加事件处理程序。比如:

this.webBrowser1.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating);

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    Console.WriteLine("Navigating to: " + e.Url);
}

您会看到有一个 JavaScript 函数不断尝试重定向页面。这是我的控制台输出中显示的内容(无限期地继续):

Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})())
Navigating to: about:blank
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})())
Navigating to: about:blank
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})())
Navigating to: about:blank
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})())

这使得 webBrowser 控件基本上无法使用。

编辑: 好的,尝试一种解决方法(这可能很糟糕,但令人沮丧的是,奇怪的重定向循环只发生在 WebBrowser 控件的浏览器中)。

如果您在另一个 Navigating 事件完成之前阻止调用 Navigating 事件,它会加载页面并且不会冻结,并且链接似乎可以工作。它是这样的:

 private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        Console.WriteLine("Navigated to: " + e.Url);
        isNavigating = false;
        webBrowser1.AllowNavigation = true;
    }

    bool isNavigating = false;
    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (isNavigating && e.Url.ToString().Contains("javascript:void((function(){document.open();document.domain='costco.com'"))
        {
            webBrowser1.Stop();
            webBrowser1.AllowNavigation = false;
            return;
        }

        isNavigating = true;
        Console.WriteLine("Navigating to: " + e.Url);
    }

【讨论】:

  • 这实际上是一个非常好的防止它冻结的方法。但是,当您停止 Web 浏览器时,似乎还有其他数据需要下载但未下载。我想知道当导航请求导致“javascript:void((function(){document.open();document.domain='costco.com”
  • 是的,如果在 IsNavigating 事件中调用 webBrowser 的 Stop() 方法,会取消导航。但是,我认为在这种情况下,如果您只是停止 JavaScript 调用,它将导致页面无法正确加载该页面。我尝试了几种停止导航到 about:blank 或您提到的 JavaScript 函数的组合;最后我最幸运的是上面的代码。
猜你喜欢
  • 2017-03-07
  • 2011-05-27
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多