C# WebBrowser控件是否忽略POST请求的302重定向?

C# WebBrowser控件是否忽略POST请求的302重定向?,c#,redirect,post,C#,Redirect,Post,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" />

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>

单击“Authorize”链接时,它通过HTTP POST将表单提交给URL2,HTTP POST以HTTP 302响应,“location”标题为“location:URL3”。我希望WebBrowser控件接受HTTP 302响应并遵循重定向链接URL3。但是,它什么也不做,只是忽略了HTTP 302,并保留在原始页面“URL1”(不是URL2也不是URL3)上。从Fiddler到URL2的POST REQUEST被标记为“会话被客户端、Fiddler或服务器中止”,即使它具有正确的302响应和正确的标题“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
this.webBrowser1
{System.Windows.Forms.WebBrowser}
基本:{System.Windows.Forms.WebBrowser}
AllowNavigation:正确
AllowWebBrowserDrop:true
坎戈巴克:错
前进:错
文档:{System.Windows.Forms.HtmlDocument}
DocumentStream:{System.IO.MemoryStream}
文档文本:“
文件标题:“测试”
文档类型:“HTM文件”
加密级别:不安全
重点:错误
IsBusy:错
伊索弗林:错
IsWebBrowserContextMenuEnabled:为真
ObjectForScript:null
填充:{Left=0,Top=0,Right=0,Bottom=0}
ReadyState:完成
ScriptErrorsSuppressed:false
ScrollBarsEnabled:true
状态文本:“
网址:{http://localhost/testform.html}
版本:{11.0.9600.17690}
WebBrowserShortcutsEnabled:正确

这是WebBrowser控件的错误吗?(是的,我已经做了注册表更改,所以WebBrowser正在IE 11模式下工作)。我该怎么做才能使WebBrowser控件遵循POST reguest中的HTTP 302重定向URL?谢谢

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

  • 处理导航事件并将“取消”设置为true
  • 启用url缓解

  • 请更换
    ,另外,Fiddler说服务器返回了什么?您是否绝对确定发送了格式正确的
    位置:
    标题的HTTP 302响应?另外,请发布配置
    WebBrowser
    实例的
    InitializeComponent
    代码。页面URL1来自我订阅的服务,我没有修改它的权限。因此,我无法删除元素:(我非常确定HTTP 302响应是好的,因为比较fiddler跟踪,无论在WebBrowser控件中还是在独立浏览器(如IE11或Chrome)中呈现,它都是相同的。在独立浏览器中呈现时,浏览器遵循“位置”中的重定向URL标题没有问题。我使用了WebBrowser实例的所有默认配置,所以只需创建实例,然后调用Navigate(url)。从调试窗口,我在下面打印了实例。。。
    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;
                }
            };