WebBrowser控件NavigateToString()无法为嵌入式Web页面激活ajax调用

时间:2014-01-30 13:35:15

标签: windows-phone-8 webbrowser-control

我的要求是在windows phone app中显示动态网页。

代码段:

    private string MYACCOUNT_URL = "http://www.abc.com"

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        try
        {
            //Initialization
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(MYACCOUNT_URL);

            WebReq.CookieContainer = cookies;

            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "application/x-www-form-urlencoded";

            //WebHeaderCollection headers = new WebHeaderCollection();
            //headers[HttpRequestHeader.UserAgent] = useragent;
            //WebReq.Headers = headers;

            //WebReq.Headers["User-Agent"] = useragent;
            //var userAgent = WebReq.GetType().GetProperty("UserAgent");
            //if (null != userAgent)
            //{
            //    userAgent.SetValue(WebReq, useragent, null);
            //}
            //WebReq.UserAgent = userAgent.ToString();

            Cookie emailCookie = new Cookie("email", Constants.LoginUserName);
            WebReq.CookieContainer.Add(new Uri(MYACCOUNT_URL), emailCookie);

            Cookie userIdCookie = new Cookie("id", Constants.UserId);
            WebReq.CookieContainer.Add(new Uri(MYACCOUNT_URL), userIdCookie);

            Cookie tokenCookie = new Cookie("token", Constants.AccessToken);
            WebReq.CookieContainer.Add(new Uri(MYACCOUNT_URL), tokenCookie);

            WebReq.BeginGetResponse(new AsyncCallback(MyAccountResponseCallback), WebReq);
        }
        catch (WebException ex)
        { }
    }  

    private void MyAccountResponseCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);                      

            string responseString = streamRead.ReadToEnd();
            responseString = Regex.Replace(responseString, "<!DOCTYPE html>", "", RegexOptions.IgnoreCase).Trim();
            responseString = Regex.Replace(responseString, "<html lang=\"en\">", "<html>", RegexOptions.IgnoreCase).Trim();
            responseString = Regex.Replace(responseString, "<meta charset=\"utf-8\"", "<meta", RegexOptions.IgnoreCase).Trim();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    web_browser.NavigateToString(responseString);
                });                                   
            }

            streamResponse.Close();
            streamRead.Close();

            // Release the HttpWebResponse
            response.Close();

        }
        catch (WebException ex)
        {
         }
    }

网页已正确加载。网页包含表单,jquery-ajax。这些事件(单击/加载)将从加载的网页触发。相同的网页在Internet Explorer中加载,并与Windows Phone 8上的所有事件一起正常工作。

请建议任何解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:1)

感谢您的回复。不幸的是“IsScriptEnabled”在这里没有用。但我得到了一个解决方案。我使用“导航”方法而不是“NavigateToString”,它正在工作。它正在加载网页,它还允许我完成网页上的所有活动。

谢谢