简体   繁体   English

如何在 C# WebBrowser 控件中获取重定向的 url

[英]How to get redirected url in C# WebBrowser Control

I have a webpage that loads, does some calculations and then does a JavaScript redirect to another webpage.我有一个加载的网页,进行一些计算,然后将 JavaScript 重定向到另一个网页。 It looks something like this:它看起来像这样:

http://www.mysite.com/startpage.html http://www.mysite.com/startpage.html

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
$(document).ready(function() {
    window.location = "http://www.mynewurl.com";
});
</head>
<body>
Something
</body>
</html>

Now I have an asp.net MVC app that loads a WebBrowser control and my goal is to be able to retrieve http://www.mynewurl.com from within my C# WebBrowser control.现在我有一个加载 WebBrowser 控件的 asp.net MVC 应用程序,我的目标是能够从我的 C# WebBrowser 控件中检索http://www.mynewurl.com

My WebBrowser code looks like:我的 WebBrowser 代码如下所示:

WebBrowser webBrowser = new WebBrowser
                                    {
                                        ScrollBarsEnabled = false,
                                        Size = new Size(Width, Height),
                                        ScriptErrorsSuppressed = true
                                    };

    webBrowser.NewWindow += WebBrowserNewWindow;
    webBrowser.Navigate("http://www.mysite.com/startpage.html");


    //wait for it to load
    while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
    {
        Application.DoEvents();
    }

    Uri myUrl = webBrowser.Url;  //This is how I have been trying to get it.

So when I load my webBrowser control I send it to http://www.mysite.com/startpage.html .所以当我加载我的 webBrowser 控件时,我将它发送到http://www.mysite.com/startpage.html It does a javascript redirect to http://www.mynewurl.com .它执行 javascript 重定向到http://www.mynewurl.com I want to somehow capture that url in my c# code.我想以某种方式在我的 c# 代码中捕获该 url。

Any ideas on how I can achieve this?关于如何实现这一目标的任何想法?

HttpWebRequest.Referer perhaps? HttpWebRequest.Referer 也许? Or maybe append whatever data you need as a query string.或者可能附加您需要的任何数据作为查询字符串。

I've decided to answer this post - even it's almost 2 years since it was posted - as it is listed in the top results in search engines (eg google), but without providing me with a solution...我决定回答这个帖子——即使它发布已经快 2 年了——因为它被列在搜索引擎(例如谷歌)的顶部结果中,但没有为我提供解决方案......

After some searching I've found a solution to this question, which can be found here:经过一番搜索,我找到了这个问题的解决方案,可以在这里找到:

http://connect.microsoft.com/VisualStudio/feedback/details/115195/webbrowser-newwindow-event-does-not-give-the-url-of-the-new-window-in-eventargs#details http://connect.microsoft.com/VisualStudio/feedback/details/115195/webbrowser-newwindow-event-does-not-give-the-url-of-the-new-window-in-eventargs#details

This solves the problem of getting the URL of the new window, when the Cancel property of CancelEventArgs is set to TRUE.这就解决了在CancelEventArgs的Cancel属性设置为TRUE时获取新窗口URL的问题。

With hope that this might help other people, with this problem, as it has not been resolved in the .NET platform ...希望这可以帮助其他人解决这个问题,因为它没有在 .NET 平台中得到解决......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM