开发者

How to get redirected url in C# WebBrowser Control

开发者 https://www.devze.com 2022-12-20 09:06 出处:网络
I have a webpage that loads, does some calculations and then does a JavaScript redirect to another webpage. It looks something like this:

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 goal is to be able to retrieve http://www.mynewurl.com from within my C# WebBrowser control.

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 (e.g. 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 ...

0

精彩评论

暂无评论...
验证码 换一张
取 消