首页 文章

WebBrowser控件忽略来自POST请求的302重定向?

提问于
浏览
3

C#windows表单应用程序包含名为browser1的System.Windows.Forms.WebBrowser控件 . 我调用browser1.Navigate(“URL1”),因此它显示页面URL1,其格式如下 . 我无权修改此页面 .

<form id="authorize" action="folder1" method="POST">
    <input type="hidden" name="code" value="198fa16c0d82" />
    <input type="hidden" id="granted" name="granted" value="false" />
    <input type="hidden" id="offlineAccess" name="offlineAccess" value="true" />
    <a href="javascript:;" onclick="document.getElementById('authorize').submit(); return true;" class="hs-button primary accept">Authorize</a>
</form>

当点击“授权”链接时,它通过HTTP POST将表单提交给URL2,HTTP POST以HTTP 302响应,其中“location” Headers 为“Location:URL3” . 我期望WebBrowser控件遵守HTTP 302响应并遵循重定向链接URL3 . 但是,它什么都不做,只是忽略了HTTP 302并保留在原始页面“URL1”(不是URL2而不是URL3) . 从Fiddler,POST请求到URL2被标记为“会话被客户端,Fiddler或服务器中止”,即使它具有正确的302响应和正确的 Headers “Location:URL3” .

然后我在同一台机器上的独立IE 11浏览器上尝试完全相同的步骤,它遵循302重定向并正确地重定向到URL3 .

WebBrowser实例使用所有默认值而无需自定义,如下所示:

this.webBrowser1
{System.Windows.Forms.WebBrowser}
    base: {System.Windows.Forms.WebBrowser}
    AllowNavigation: true
    AllowWebBrowserDrop: true
    CanGoBack: false
    CanGoForward: false
    Document: {System.Windows.Forms.HtmlDocument}
    DocumentStream: {System.IO.MemoryStream}
    DocumentText: "<omitted>"
    DocumentTitle: "test"
    DocumentType: "HTM File"
    EncryptionLevel: Insecure
    Focused: false
    IsBusy: false
    IsOffline: false
    IsWebBrowserContextMenuEnabled: true
    ObjectForScripting: null
    Padding: {Left=0,Top=0,Right=0,Bottom=0}
    ReadyState: Complete
    ScriptErrorsSuppressed: false
    ScrollBarsEnabled: true
    StatusText: ""
    Url: {http://localhost/testform.html}
    Version: {11.0.9600.17690}
    WebBrowserShortcutsEnabled: true

这是WebBrowser控件的错误吗? (是的,我已经进行了注册表更改,因此WebBrowser正在IE 11模式下工作) . 我可以做什么,以便WebBrowser控件可以遵循来自POST reguest的HTTP 302重定向URL?谢谢!

1 回答

  • 0

    有两种方法可以解决这个问题

    • 处理导航事件并将取消设置为true .

    .

    webBrowserControl.Navigating += (sender, args) =>
            {
                //support redirect via 302 response by ignoring href of the authorise button click.    
                if (args.Url.AbsoluteUri.StartsWith("javascript:"))
                {
                    args.Cancel = true;
                }
            };
    
    • 启用网址缓解

    WebBrowser control does not complete loading when javascript:void(0) called

相关问题