0

Ok so I have a website created in ASP.NET Web applications (web forms) and UWP app. I have a gridview webform page in my website. I want this gridview to be retrieved to my UWP app. Currently, I've tried to use HTTP Client service which i'm also not too sure how it works tbh. I'm actually very confused with coding ;) i'm very new to programming :)

Ad.xaml:

<Page
    x:Class="TMD_Display.Views.Ad"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TMD_Display.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:models="using:TMD_Display.Models"
    xmlns:fileproperties="using:Windows.Storage.FileProperties"
    mc:Ignorable="d" Loaded="Page_Loaded"
    Height="600">

    <Grid Name="myGrid">
        <GridView >

        </GridView>
    </Grid>

</Page>

Ad.xaml.cs:

    public sealed partial class Ad : Page
    {
    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        GetGrid();
    }



    private async void GetGrid()
    {
        //Create an HTTP client object
        Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();

        //Add a user-agent header to the GET request. 
        var headers = httpClient.DefaultRequestHeaders;

        //The safe way to add a header value is to use the TryParseAdd method and verify the return value is true,
        //especially if the header value is coming from user input.
        string header = "ie";
        if (!headers.UserAgent.TryParseAdd(header))
        {
            throw new Exception("Invalid header value: " + header);
        }


        header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
        if (!headers.UserAgent.TryParseAdd(header))
        {
            throw new Exception("Invalid header value: " + header);
        }

        Uri requestUri = new Uri("http://localhost:7665/AdvGridView.aspx");

        //Send the GET request asynchronously and retrieve the response as a string.
        Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
        string httpResponseBody = "";

        try
        {
            //Send the GET request
            httpResponse = await httpClient.GetAsync(requestUri);
            httpResponse.EnsureSuccessStatusCode();
            httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
        }
    }
}
  • What isn't working? Does `httpResponseBody` not contain what you expect? As a general word of advice, if you control the web application then scraping the HTML is just giving yourself way too much work and too much potential for error. You can create a separate service in your web application which returns the same data in a structured format (JSON usually). Then your client application can call that service and just get the data, without all the messy HTML it doesn't need. – David Jan 30 '19 at 15:10
  • Use a sniffer like wireshark or fiddle and connect to website using a browser. Capture results and then do same with your application. Make sure you app contains the same headers as browser. – jdweng Jan 30 '19 at 15:11
  • What build of Windows 10 are you running? I believe this restriction got lifted at some point, but will need to dig up details. Fwiw, your code seems to work for me on the 1809 update (build 17763). – Stefan Wick MSFT Jan 30 '19 at 21:40
  • @StefanWickMSFT Yes i'm also running on 1809 Update (build 17763) – Alisa Fathima Jan 31 '19 at 00:39
  • What part exactly isn't working? What error are you getting? – Stefan Wick MSFT Jan 31 '19 at 00:44

1 Answers1

0

It's a feature of UWP called netisolation, this prohibits loopback calls.

More info on how to enable it can be found here.

UWP Enable Local Network

YMDW
  • 411
  • 1
  • 5
  • 16