C#web浏览器的Ajax调用 [英] C# webbrowser Ajax call

查看:177
本文介绍了C#web浏览器的Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#WPF应用程序.NET4内嵌WebBrowser控件。每当我preSS手动按钮的形式,浏览器挂在您的要求正在处理的消息,并没有任何反应。如果我这样做完全IE浏览器的页面处理正常生产的结果。
我很想念?谢谢
该按钮背后的code是以下

 <一的onclick =开始搜索(); HREF =JavaScript的:无效(-1); NAME =btnN​​ext级=BTN floatLe noClear btnSubmit按钮btnRight>
<跨度>继续< / SPAN>
&所述; / A>


解决方案

更新,我目前使用和推荐一套可以发现的这里


WebBrowser控件(WPF和WinForms的版本)中的行为不同于IE全面多种方式。你可能想实现功能控制,使其行为接近IE越好(特别是 FEATURE_BROWSER_EMULATION )。我们希望,这个问题便会消失。下面是一些code:

 私人无效SetBrowserFeatureControlKey(字符串特征串的appName,UINT值)
{
    使用(VAR键= Registry.CurrentUser.CreateSubKey(
        String.Concat(@软件\\微软\\的Internet Explorer \\ MAIN \\ FeatureControl \\功能),
        RegistryKeyPermissionCheck.ReadWriteSubTree))
    {
        key.SetValue(的appName,(UInt32的)值,RegistryValueKind.DWord);
    }
}

例如:

 私人无效SetBrowserFeatureControl()
{
    // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx    // FeatureControl设置是每个进程
    变种文件名= System.IO.Path.GetFileName(Process.GetCurrentProcess()MainModule.FileName);    //使控制不Visual Studio设计中运行
    如果(的String.Compare(文件名,devenv.exe的,真)== 0 ||的String.Compare(文件名,XDesProc.exe,真)== 0)
        返回;    SetBrowserFeatureControlKey(FEATURE_BROWSER_EMULATION,文件名,GetBrowserEmulationMode()); //网页包含基于标准!​​DOCTYPE指令显示在IE10标准模式。
    SetBrowserFeatureControlKey(FEATURE_AJAX_CONNECTIONEVENTS,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_DOMSTORAG​​E,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_GPU_RENDERING,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI,文件名,0);
    SetBrowserFeatureControlKey(FEATURE_DISABLE_LEGACY_COM preSSION,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_LOCALMACHINE_LOCKDOWN,文件名,0);
    SetBrowserFeatureControlKey(FEATURE_BLOCK_LMZ_OBJECT,文件名,0);
    SetBrowserFeatureControlKey(FEATURE_BLOCK_LMZ_SCRIPT,文件名,0);
    SetBrowserFeatureControlKey(FEATURE_DISABLE_NAVIGATION_SOUNDS,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_SCRIPTURL_MITIGATION,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_SPELLCHECKING,文件名,0);
    SetBrowserFeatureControlKey(FEATURE_STATUS_BAR_THROTTLING,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_TABBED_BROWSING,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_VALIDATE_NAVIGATE_URL,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_WEBOC_DOCUMENT_ZOOM,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_WEBOC_POPUPMANAGEMENT,文件名,0);
    SetBrowserFeatureControlKey(FEATURE_WEBOC_MOVESIZECHILD,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_ADDON_MANAGEMENT,文件名,0);
    SetBrowserFeatureControlKey(FEATURE_WEBSOCKET,文件名,1);
    SetBrowserFeatureControlKey(FEATURE_WINDOW_RESTRICTIONS,文件名,0);
    SetBrowserFeatureControlKey(FEATURE_XMLHTTP,文件名,1);
}私人UInt32的GetBrowserEmulationMode()
{
    INT browserVersion = 7;
    使用(VAR ieKey = Registry.LocalMachine.OpenSubKey(@SOFTWARE \\微软\\ Internet Explorer的,
        RegistryKeyPermissionCheck.ReadSubTree,
        System.Security.AccessControl.RegistryRights.QueryValues​​))
    {
        VAR的版本= ieKey.GetValue(svcVersion);
        如果(空==版)
        {
            版本= ieKey.GetValue(版本);
            如果(空==版)
                抛出新ApplicationException的(Microsoft Internet Explorer中是必需的!);
        }
        int.TryParse(version.ToString()斯普利特()[0],出browserVersion'。');
    }    UInt32的模式= 11000; // Internet Explorer的11网页包含基于标准!​​DOCTYPE指令显示在IE11标准模式。适用于Internet Explorer 11的默认值。
    开关(browserVersion)
    {
        案例7:
            模式= 7000; //网页包含基于标准!​​DOCTYPE指令显示在IE7标准模式。对于承载WebBrowser控件的应用程序的默认值。
            打破;
        案例8:
            模式= 8000; //网页包含基于标准!​​DOCTYPE指令显示在IE8模式。 Internet Explorer 8的默认值
            打破;
        案例9:
            模式= 9000; //的Internet Explorer 9的网页包含基于标准!​​DOCTYPE指令显示在IE9模式。针对Internet Explorer 9的默认值。
            打破;
        案例10:
            模式= 10000; // Internet Explorer的10网页包含基于标准!​​DOCTYPE指令显示在IE10模式。适用于Internet Explorer 10的默认值。
            打破;
        默认:
            //默认使用IE11模式
            打破;
    }    返回模式;
}

您应该拿出自己的设置功能并注册在他们面前的WebBrowser已初始化,例如,在主窗体的构造函数:

 公共主窗口()
{
    SetBrowserFeatureControl();    的InitializeComponent();
// ...
}

I am using a webbrowser control embeded in C# WPF .NET4 app. Whenever I press manually the button in a form, the browser hangs on "Your request is being processed" message and nothing happens. If I do the same in full IE browser the page is processed normally producing the results. What I am missing? Thanks The code behind the button is the following

<a onclick="startSearch();" href="javascript:void(-1);" name="btnNext" class="btn floatLe noClear btnSubmit btnRight"> 
<span>Continue</span> 
</a>

解决方案

Updated, I currently use and recommend a set of features that can be found here.


WebBrowser control (both WPF and WinForms versions) behaves in many ways differently from the full IE. You may want to implement Feature Control to bring its behavior as close to IE as possible (particularly, FEATURE_BROWSER_EMULATION). Hopefully, the problem will then be gone. Here is some code:

private void SetBrowserFeatureControlKey(string feature, string appName, uint value)
{
    using (var key = Registry.CurrentUser.CreateSubKey(
        String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature), 
        RegistryKeyPermissionCheck.ReadWriteSubTree))
    {
        key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
    }
}

For example:

private void SetBrowserFeatureControl()
{
    // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx

    // FeatureControl settings are per-process
    var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

    // make the control is not running inside Visual Studio Designer
    if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0) 
        return;

    SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode()); // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
    SetBrowserFeatureControlKey("FEATURE_AJAX_CONNECTIONEVENTS", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_DOMSTORAGE ", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_GPU_RENDERING ", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI  ", fileName, 0);
    SetBrowserFeatureControlKey("FEATURE_DISABLE_LEGACY_COMPRESSION", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_LOCALMACHINE_LOCKDOWN", fileName, 0);
    SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_OBJECT", fileName, 0);
    SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_SCRIPT", fileName, 0);
    SetBrowserFeatureControlKey("FEATURE_DISABLE_NAVIGATION_SOUNDS", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_SCRIPTURL_MITIGATION", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_SPELLCHECKING", fileName, 0);
    SetBrowserFeatureControlKey("FEATURE_STATUS_BAR_THROTTLING", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_TABBED_BROWSING", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_VALIDATE_NAVIGATE_URL", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_WEBOC_DOCUMENT_ZOOM", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 0);
    SetBrowserFeatureControlKey("FEATURE_WEBOC_MOVESIZECHILD", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_ADDON_MANAGEMENT", fileName, 0);
    SetBrowserFeatureControlKey("FEATURE_WEBSOCKET", fileName, 1);
    SetBrowserFeatureControlKey("FEATURE_WINDOW_RESTRICTIONS ", fileName, 0);
    SetBrowserFeatureControlKey("FEATURE_XMLHTTP", fileName, 1);
}

private UInt32 GetBrowserEmulationMode()
{
    int browserVersion = 7;
    using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
        RegistryKeyPermissionCheck.ReadSubTree,
        System.Security.AccessControl.RegistryRights.QueryValues))
    {
        var version = ieKey.GetValue("svcVersion");
        if (null == version)
        {
            version = ieKey.GetValue("Version");
            if (null == version)
                throw new ApplicationException("Microsoft Internet Explorer is required!");
        }
        int.TryParse(version.ToString().Split('.')[0], out browserVersion);
    }

    UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. Default value for Internet Explorer 11.
    switch (browserVersion)
    {
        case 7:
            mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.
            break;
        case 8:
            mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
            break;
        case 9:
            mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
            break;
        case 10:
            mode = 10000; // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 mode. Default value for Internet Explorer 10.
            break;
        default:
            // use IE11 mode by default
            break;
    }

    return mode;
}

You should come up with your own set of features and register them before WebBrowser has initialized, e.g., in the main form constructor:

public MainWindow()
{
    SetBrowserFeatureControl();

    InitializeComponent();
//...
}

这篇关于C#web浏览器的Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆