1

我正在尝试检索输入并提交正确的用户名和密码后返回的用户 ID。

我一直在努力:

  1. 成功登录后关闭 Web 浏览器。(它只是重定向到具有 UserID 的页面,并且最后具有带有用户 ID 的 URL。

  2. 登录后检索用户 ID。

我试过这个来检索用户ID,但没有成功。

        OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs ex)
                {
                base.OnNavigatedTo(ex); 
                IDictionary<string, string> parameters = NavigationContext.QueryString;      //once the login is successful this piece of code should be 
                 if (parameters.ContainsKey("id"))                                          //executed 
                     {
                    string UserId = parameters["id"];
                    }
                }

然后我尝试了以下方法。我得到了当前的 URL,而不是加载的 URL。除此之外,我如何解析它以获取用户 ID?

private void webBrowser2_Navigated(object sender, NavigationEventArgs e)
        {
 string URLString = e.Uri.ToString();
           txtURL.Text = URLString;

我想我可能只是在事件处理程序和解析方面遇到了问题。

对不起,我是新手,所以请原谅我犯的任何错误。

谢谢。

4

1 回答 1

0

The Navigated event will fire each time a new page is loaded in the control. So the event will be fired first when your login page is navigated to, you would ignore this. The next time the event will fire as a result of navigation in the control and this time will either be from the user successfully logging in or a failure. Either way you'll need to read the Uri and parse out the relevant parameters. You can extract just the query elements from the uri using the Query property. See here for details http://msdn.microsoft.com/en-us/library/system.uri.query(v=vs.95).aspx

于 2013-04-11T22:16:49.417 回答