好,所以我有一个在ASP.NET Web应用程序(Web表单)和UWP应用程序中创建的网站。我的网站上有一个gridview Webform页面。我希望将此gridview检索到我的UWP应用中。目前,我尝试使用HTTP客户端服务,但我也不太清楚它的工作方式。我实际上对编码很困惑;)我对编程很陌生:)
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;
}
}
}