Skip to main content
 首页 » 编程设计

c#之当我尝试使用 Web 浏览器 C# 打开网站时没有任何反应

2024年04月17日44zhoujg

我是 C# 的菜鸟,我正在尝试编写一个程序来使用 web 浏览器 打开网站。它适用于 Google、Facebook 和其他网站,但当我尝试打开网站 https://shopee.ph/ 时没有任何反应。

这是我的代码

webBrowser1.Navigate("https://shopee.ph/"); 

有什么解决办法吗?

请您参考如下方法:

本站https://shopee.ph/正在检查浏览器版本,如果不支持则重定向到 shopee.ph/unsupported.html .默认 WebBrowser控件使用的是您提到的网站不支持的 IE7。 如前所述,有几个选项可以更改 WebBrowser 的浏览器 here .我已经尝试过其中之一并按预期工作。以下是完整代码:

using Microsoft.Win32; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Security; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
 
namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
        InitializeComponent(); 
    } 
 
    private void Form1_Load(object sender, EventArgs e) 
    { 
        if (!WBEmulator.IsBrowserEmulationSet()) 
        { 
            WBEmulator.SetBrowserEmulationVersion(); 
        } 
 
        webBrowser1.AllowNavigation = true; 
        webBrowser1.Navigate("https://shopee.ph/"); 
    } 
} 
 
public enum BrowserEmulationVersion 
{ 
    Default = 0, 
    Version7 = 7000, 
    Version8 = 8000, 
    Version8Standards = 8888, 
    Version9 = 9000, 
    Version9Standards = 9999, 
    Version10 = 10000, 
    Version10Standards = 10001, 
    Version11 = 11000, 
    Version11Edge = 11001 
} 
public static class WBEmulator 
{ 
    private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer"; 
 
    public static int GetInternetExplorerMajorVersion() 
    { 
        int result; 
 
        result = 0; 
 
        try 
        { 
            RegistryKey key; 
 
            key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey); 
 
            if (key != null) 
            { 
                object value; 
 
                value = key.GetValue("svcVersion", null) ?? key.GetValue("Version", null); 
 
                if (value != null) 
                { 
                    string version; 
                    int separator; 
 
                    version = value.ToString(); 
                    separator = version.IndexOf('.'); 
                    if (separator != -1) 
                    { 
                        int.TryParse(version.Substring(0, separator), out result); 
                    } 
                } 
            } 
        } 
        catch (SecurityException) 
        { 
            // The user does not have the permissions required to read from the registry key. 
        } 
        catch (UnauthorizedAccessException) 
        { 
            // The user does not have the necessary registry rights. 
        } 
 
        return result; 
    } 
    private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION"; 
 
    public static BrowserEmulationVersion GetBrowserEmulationVersion() 
    { 
        BrowserEmulationVersion result; 
 
        result = BrowserEmulationVersion.Default; 
 
        try 
        { 
            RegistryKey key; 
 
            key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true); 
            if (key != null) 
            { 
                string programName; 
                object value; 
 
                programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]); 
                value = key.GetValue(programName, null); 
 
                if (value != null) 
                { 
                    result = (BrowserEmulationVersion)Convert.ToInt32(value); 
                } 
            } 
        } 
        catch (SecurityException) 
        { 
            // The user does not have the permissions required to read from the registry key. 
        } 
        catch (UnauthorizedAccessException) 
        { 
            // The user does not have the necessary registry rights. 
        } 
 
        return result; 
    } 
    public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion) 
    { 
        bool result; 
 
        result = false; 
 
        try 
        { 
            RegistryKey key; 
 
            key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true); 
 
            if (key != null) 
            { 
                string programName; 
 
                programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]); 
 
                if (browserEmulationVersion != BrowserEmulationVersion.Default) 
                { 
                    // if it's a valid value, update or create the value 
                    key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord); 
                } 
                else 
                { 
                    // otherwise, remove the existing value 
                    key.DeleteValue(programName, false); 
                } 
 
                result = true; 
            } 
        } 
 
        catch (UnauthorizedAccessException) 
        { 
            // The user does not have the necessary registry rights. 
        } 
 
        return result; 
    } 
 
    public static bool SetBrowserEmulationVersion() 
    { 
        int ieVersion; 
        BrowserEmulationVersion emulationCode; 
 
        ieVersion = GetInternetExplorerMajorVersion(); 
 
        if (ieVersion >= 11) 
        { 
            emulationCode = BrowserEmulationVersion.Version11; 
        } 
        else 
        { 
            switch (ieVersion) 
            { 
                case 10: 
                    emulationCode = BrowserEmulationVersion.Version10; 
                    break; 
                case 9: 
                    emulationCode = BrowserEmulationVersion.Version9; 
                    break; 
                case 8: 
                    emulationCode = BrowserEmulationVersion.Version8; 
                    break; 
                default: 
                    emulationCode = BrowserEmulationVersion.Version7; 
                    break; 
            } 
        } 
 
        return SetBrowserEmulationVersion(emulationCode); 
    } 
    public static bool IsBrowserEmulationSet() 
    { 
        return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default; 
    } 
} 
} 

希望它能解决您的问题。