【发布时间】:2015-03-30 22:52:32
【问题描述】:
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 302 响应,其中“location”标头为“Location: URL3”。我希望 WebBrowser 控件能够接受 HTTP 302 响应并遵循重定向链接 URL3。但是,它什么也不做,只是忽略了 HTTP 302 并保留在原始页面“URL1”(不是 URL2 和不是 URL3)上。从 Fiddler 到 URL2 的 POST 请求被标记为“会话已被客户端、Fiddler 或服务器中止”,即使它具有正确的 302 响应以及正确的标头“位置: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?谢谢!
【问题讨论】:
-
请用简单的
<button type="submit">Authorize</button>替换<a href(使用不必要的复杂内联Javascript),这样更容易调查。 -
另外,Fiddler 说服务器返回了什么?您是否绝对确定正在发送带有正确格式的
Location:标头的 HTTP 302 响应? -
另外,请发布您的
InitializeComponent代码以配置您的WebBrowser实例。 -
页面 URL1 来自我订阅的服务,我无权修改它。所以我不能删除 元素:(
-
我很确定 HTTP 302 响应很好,因为比较提琴手跟踪,无论在 WebBrowser 控件或在独立浏览器(如 IE11 或 Chrome)中呈现,它都是相同的。在独立浏览器中呈现时,浏览器会毫无问题地遵循“位置”标头中的重定向 URL。我使用了 WebBrowser 实例的所有默认配置,因此只需创建实例,然后调用 Navigate(url)。在调试窗口中,我打印了下面的实例...