locked
How to share Cookie between HttpWebRequest and WebView Control for Windows Phone 8.1

    Question

  • Hello All,

    I am using HttpWebRequest to get the html code for a page, and then modify it by removing some parts, and modifying it relative link to absolute ones in order to let the WebView control to load image automatically and correctly. But without passing the cookie from the HttpWebRequest to the WebView, the image can't be retrieved . So my question is how to share the cookie between these two classes?

    I used Httpclient, and people say it will automatically solute my problem, but it does not. here is my code, the webview still not display the image .

    try
                   {
                       HttpClient client = new HttpClient();
                       String responseFromServer = await client.GetStringAsync(rootUrl);
                       responseFromServer = ModifyHTMLCode(responseFromServer);
                  
                       webView.NavigateToString(responseFromServer);
                       client.Dispose();
                   }
                   catch
                   {
    
                   }


    Thanks!


    Never Give Up!



    • Edited by Chunde Huang Wednesday, November 26, 2014 9:28 AM
    Wednesday, November 26, 2014 9:10 AM

Answers

  • NavigateToString won't use a cookie to download resources as you're not downloading content from a server.  In fact, I am not sure that the cookie is your problem - NavigateToString is known to not get content such as images because the image isn't part of the request.  I think you need to use NavigateToLocalStreamUri - please see my blog post about this:

    http://blogs.msdn.com/b/wsdevsol/archive/2014/06/20/a-primer-on-webview-navigatetolocalstreamuri.aspx

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, November 26, 2014 1:31 PM
    Moderator
  • Make sure you are using Windows.Web.Http.HttpClient and not System.Net.Http.HttpClient.

    You can sniff in the cookies by using the HttpCookieManager class. 

    Wednesday, November 26, 2014 9:25 PM

All replies

  • NavigateToString won't use a cookie to download resources as you're not downloading content from a server.  In fact, I am not sure that the cookie is your problem - NavigateToString is known to not get content such as images because the image isn't part of the request.  I think you need to use NavigateToLocalStreamUri - please see my blog post about this:

    http://blogs.msdn.com/b/wsdevsol/archive/2014/06/20/a-primer-on-webview-navigatetolocalstreamuri.aspx

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, November 26, 2014 1:31 PM
    Moderator
  • Make sure you are using Windows.Web.Http.HttpClient and not System.Net.Http.HttpClient.

    You can sniff in the cookies by using the HttpCookieManager class. 

    Wednesday, November 26, 2014 9:25 PM