在WP7和&amp ;;中的httpwebresponse的WebBrowser控件中加载问题8

时间:2014-08-08 12:14:30

标签: windows-phone-7 windows-phone-8 httpwebrequest webbrowser-control httpwebresponse

我正在尝试使用httpwebresponse中的html代码加载Web浏览器,方法是将其存储在字符串变量中。

我通过在其中设置Cookie并在httpwebResponse中获取html页面来为URL创建httpwebrequest。

我试图通过将代码保存在字符串变量中来在Web浏览器控件中显示它。并浏览浏览器(“browse.NavigateToString(string)”)。

但是此页面未正确加载。它没有显示任何CSS或图像并显示消息“您需要打开java脚本才能显示内容”。

我在导航之前使网页浏览器属性(IsScriptEnabled)为true。但它仍然显示消息和内容未正确加载。

Cookie也不会注入Web浏览器控件(会话不会通过应用程序进行维护)。

是否有任何解决方案或解决方法?

请参考以下代码: 从登录服务记录Cookie:

private void PostLoginRequest()         {

        string AuthServiceUri = "Service URL";
        HttpWebRequest AuthReq = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest;
        AuthReq.CookieContainer = new CookieContainer();
        AuthReq.ContentType = "application/x-www-form-urlencoded";
        AuthReq.Method = "POST";

        AuthReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), AuthReq);
    }

    void GetRequestStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest Request = (HttpWebRequest)callbackResult.AsyncState;

        Stream postStream = Request.EndGetRequestStream(callbackResult);
        string postData = "Postdata parameters";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();
        myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), Request);

    }

    void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {

        try
        {

            HttpWebRequest callBackRequest = (HttpWebRequest)callbackResult.AsyncState;

            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);

            CookieCollection cookiecollec = new CookieCollection();
            cookiecollec = response.Cookies;
            App.cookieCollection = cookiecollec; //global varaiable

        }
        catch (Exception e)
        {
    MessageBox.Show(e.Message);
        }
    }

另一个带有已记录Cookie的httpwebrequest:

private void ResponsivePostURL(string PageName,string PostURL,string PostDatas)              {

                    Uri ServiceUri = new Uri(PostURL); //Service URL

                    HttpWebRequest requestURL = HttpWebRequest.CreateServiceUri as HttpWebRequest;
                    requestURL.ContentType = "application/x-www-form-urlencoded";
                    requestURL.Method = "POST";                        
                    requestURL.CookieContainer = new CookieContainer();
                    requestURL.CookieContainer.Add(ServiceUri, App.cookieCollection);

                    requestURL.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), requestURL); 


             }

void GetResponsetStreamCallback(IAsyncResult callbackResult)              {

             try
             {

                 HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
                 HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);


                // MessageBox.Show(response.Cookies.ToString());

                 string responseString = "<html><head></head><body>";
                 Stream streamResponse = response.GetResponseStream();
                 StreamReader reader = new StreamReader(streamResponse);
                 responseString = responseString + reader.ReadToEnd() + "</body></html>";                   
                 streamResponse.Close();
                 reader.Close();
                 response.Close();



                 this.Dispatcher.BeginInvoke(() =>
                 {

                     webBrowser.IsScriptEnabled = true;
                     webBrowser.NavigateToString(responseString);

                 });
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }

1 个答案:

答案 0 :(得分:0)

当您导航到网站并保存文档时,您没有收到scriptcssimage文件(以及任何其他资源)但只有静态文件/生成HTML文档,可能是指使用相对路径的那些文件。如果路径不是绝对路径,则找不到脚本/ css / images /其他资源。

如果要使其工作,则需要用HTML文件中的绝对路径替换相对路径。

所以,如果你有

<script src="/scripts/cool.js"></script>

您可以用

替换它

<script src="http://www.thesite.com/scripts/cool.js"></script>

(请注意,这只是我最好的猜测,而不是我实际尝试过的东西:)