簡體   English   中英

C#Web瀏覽器保持凍結,但常規IE9瀏覽器不凍結

[英]c# Web browser keeps freezing but regular IE9 browser does not

我將Visual Studio c#2010用於網絡瀏覽器。

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#程序中的Web瀏覽器導航到此鏈接時,它會凍結。

如何防止凍結? 該網頁似乎正在從另一個站點調用一些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控件本身,而在於該特定網站如何試圖執行陷入循環的Javascript。

比較和對比:

1)將網址更改為http://google.com 工作正常。

2)現在。 為導航事件添加事件處理程序。 就像是:

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控件的瀏覽器中發生,這是令人沮喪的)。

如果您阻止在另一個導航事件完成之前調用導航事件,它將加載頁面並且不會凍結,並且鏈接似乎可以正常工作。 它是這樣的:

 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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM