Как подключить Localhost (сайт) к приложению UWP? - PullRequest
0 голосов
/ 30 января 2019

Хорошо, у меня есть веб-сайт, созданный в веб-приложениях ASP.NET (веб-формах) и приложении UWP.У меня есть страница веб-формы gridview на моем веб-сайте.Я хочу, чтобы эта сетка была загружена в мое приложение UWP.В настоящее время я пытался использовать службу HTTP Client, но я также не очень уверен, как она работает.Я на самом деле очень запутался с кодированием;) Я очень новичок в программировании:)

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;
        }
    }
}

1 Ответ

0 голосов
/ 30 января 2019

Это функция UWP, называемая netisolation, которая запрещает петлевые вызовы.

Более подробную информацию о том, как включить ее, можно найти здесь.

UWP Enable Local Network

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...