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

标签 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" />
    <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,它以“位置” header 为“位置:URL3”的 HTTP 302 响应。我希望 WebBrowser 控件接受 HTTP 302 响应并遵循重定向链接 URL3。但是,它什么也不做,只是忽略了 HTTP 302 并保留在原始页面“URL1”(不是 URL2 和 NOT URL3)上。在 Fiddler 中,对 URL2 的 POST 请求被标记为“ session 被客户端、Fiddler 或服务器中止”,即使它有正确的 302 响应和正确的 header “位置: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 请求的 HTTP 302 重定向 URL?谢谢!

最佳答案

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

  1. 处理 Navigating 事件并将取消设置为 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;
            }
        };
  1. 启用 url 缓解

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

关于c# - WebBrowser 控件忽略来自 POST 请求的 302 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29357729/

相关文章:

php - 默认 404 错误页面而不是自定义?

Android 和 RESTful 服务

ios - 使用 NSURL 发布图像文件和字符串文本参数

c# - 我可以像在 C/C++ 中那样安全地隐藏 c# exe 的加密 key (以一种无法以任何已知方式反编译的方式)吗?

c# - 'Emgu.CV.Util.CvException' 发生在 'Emgu.CV.World.dll' - 尝试预测时

redirect - Scrapy 处理 302 响应代码

php - HTTP发布到MySQL数据库INSERT

c# - 无法在第二台计算机上解密数据

c# - 在弹出窗口中使用 Owin 外部登录的 Asp.net 登录提供程序

.htaccess - 重定向不再存在的图片网址