简体   繁体   中英

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. It looks something like this:

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 is to be able to retrieve http://www.mynewurl.com from within my C# WebBrowser control.是能够从我的 C# WebBrowser 控件中检索http://www.mynewurl.com

My WebBrowser code looks like:

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 . It does a javascript redirect to http://www.mynewurl.com . I want to somehow capture that url in my c# code.

Any ideas on how I can achieve this?

HttpWebRequest.Referer perhaps? 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...

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

This solves the problem of getting the URL of the new window, when the Cancel property of CancelEventArgs is set to TRUE.

With hope that this might help other people, with this problem, as it has not been resolved in the .NET platform ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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