locked
WebView | unexpected UserAgent after using ImageControl with http path | Windows 8.1

    Question

  • After trying multiple approaches to load content in web view, following issue is noted:

    Provide Wrong User-Agent if
    1. We are using the image control to render the image by providing the URL to source property of image control  at multiple places in application.
    2. After rendering that image, web view makes it's request and provide the wrong user agent.

        User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko

    Provide Correct User-Agent if
    1. We don't perform the step no. 1 in first case. We got the user agent which is actually required.
        

    User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; WebView/2.0; rv:11.0) like Gecko

    The image url request and web view's internal request, both are totally independent and having no relation.

    The difference between two user agent is the missing part of WebView/2.0 in wrong user-agent which is actually important for us.

    Logically this should not work this way. We are unable to figure out why this is happening.
    Tuesday, September 30, 2014 10:59 AM

All replies

  • Let me see if I can get this straight:

    1) When you set the source of an Image control to a URI from the web, then refer to that same image inside a WebView control, the request for that source does not contain the part "WebView 2.0" inside the User-Agent String.

    2) If you do not use the same source previously in a URI from the web, then the WebView includes "WebView 2.0" inside the User-Agent String.

    Can you provide the source so this can be investigated?  Also, why is this important?


    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.

    Tuesday, September 30, 2014 1:04 PM
    Moderator
  • Here is the XAML - 

    <Page
        x:Name="pageRoot"
        x:Class="HS18.UI.Win8._1.TestPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:HS18.UI.Win8._1"
        xmlns:common="using:HS18.UI.Win8._1.Common"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:uc="using:HS18.UI.Win8._1.UserControls"
        xmlns:VM="using:HS18.Common.ViewModels"
        xmlns:flyouts="using:HS18.UI.Win8._1.Flyouts"
        mc:Ignorable="d">
    
        <Page.Resources>
            <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
            <x:String x:Key="AppName">My Application</x:String>
        </Page.Resources>
    
    
    
        <Grid Background="Black">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
    
            <TextBlock Grid.Row="0" Text="test Page" Foreground="Red" FontSize="50"/>
    <Image Grid.Row="1" Source="http://r3.whistleout.com/public/images/phones/HTC-Windows-Phone-8X/HTC-Windows-Phone-8X-2-xl.jpg"
    Height="100"/>
                    <WebView Grid.Row="2" Name="webTest" NavigationCompleted="webTest_NavigationCompleted"/>
        </Grid>
        
    </Page>

    CodeBehind - 

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Popups;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237
    
    namespace HS18.UI.Win8._1
    {
    
        public sealed partial class TestPage : Page
        {
            public TestPage()
            {
                this.InitializeComponent();
                SetWebView();
            }
            
            private void SetWebView()
            {
                webTest.Height = Window.Current.Bounds.Height * 0.8-150;
                webTest.Width = Window.Current.Bounds.Width * 0.9;
    
                webTest.NavigateToString(@"<html>
        <head>
            <script type='text/javascript'>
                function doSomething(customArgs) 
                { 
                    document.getElementById('sampleInput').value = customArgs;
                    document.getElementById('myForm').submit();
                    return navigator.userAgent; 
                }
            </script>
        </head>
        <body>
            <form id='myForm' action='<copy any url here>' method='post'>
                <input type='hidden' id='sampleInput' name='sampleInput' />
            </form>
        </body>
    </html>");
            }
    
             private async void webTest_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
            {
                string result = await webTest.InvokeScriptAsync("doSomething", new string[] { "My name is Manik" });
    
                Debug.WriteLine(result);
            }
    
       }
    }

    Observe the User Agent of Webview with and without Image Control.

    Wednesday, October 01, 2014 8:30 AM
  • Hi Manik - I can't get your app to work, as simple as it is. Can you post the app to OneDrive and paste a link to it here?

    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, October 01, 2014 4:23 PM
    Moderator