我如何从 Web api 获取数据到 C# windows 表单应用程序 class
How do i get data from a web api into a c# windows forms app class
我正在尝试制作一个 windows 形式的程序,可以通过网络 api 搜索电影和连续剧。然后程序需要将所有相关信息显示到表单上。
我无法让程序从 Web 读取 api。经过大量搜索后,我决定把它放在这里。
我有一些代码主要基于这篇文章:https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new App());
}
static HttpClient client = new HttpClient();
static async Task RunAsync()
{
// Update port # in the following line.
client.BaseAddress = new Uri("http://www.omdbapi.com/?apikey=caa4fbc9&t=it");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
static async Task<Film> GetFilmAsync(string path)
{
Film film = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
film = await response.Content.ReadAsAsync<Film>();
}
return film;
}
}
遗憾的是,它并没有做太多,我也不知道下一步该做什么。所以正如我所说,这应该从网络 api(http://www.omdbapi.com/) 读取数据。然后它必须将选定的项目放入 class 中,然后我可以用它来组成表格。
public class Film
{
public string Title { get; set; }
public string Released { get; set; }
public string Runtime { get; set; }
public string Genre { get; set; }
public string Director { get; set; }
public string Actors { get; set; }
public string Plot { get; set; }
public string Language { get; set; }
public string imdbRating { get; set; }
public string totalSeasons { get; set; }
public string Type { get; set; }
}
希望大家帮帮忙!不胜感激!
我对你的代码做了一些修改,它似乎运行良好:
async void Main()
{
await InitClient();
// Pass the file title to the API
var result = await GetFilmAsync("it");
client.CancelPendingRequests();
client.Dispose();
}
// Changed name to be more meaningful
static async Task InitClient()
{
// Remove the part with your key and the film title from the general initialization
client.BaseAddress = new Uri("http://www.omdbapi.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
static async Task<Film> GetFilmAsync(string title)
{
Film film = null;
// Compose the path adding the key and the film title
string path = "?apikey=caa4fbc9&t=" + title;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
string data = await response.Content.ReadAsStringAsync();
// This is the key point, the API returns a JSON string
// You need to convert it to your Film object.
// In this example I have used the very useful JSon.NET library
// that you can easily install with NuGet.
film = JsonConvert.DeserializeObject<Film>(data);
}
return film;
}
我正在尝试制作一个 windows 形式的程序,可以通过网络 api 搜索电影和连续剧。然后程序需要将所有相关信息显示到表单上。
我无法让程序从 Web 读取 api。经过大量搜索后,我决定把它放在这里。
我有一些代码主要基于这篇文章:https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new App());
}
static HttpClient client = new HttpClient();
static async Task RunAsync()
{
// Update port # in the following line.
client.BaseAddress = new Uri("http://www.omdbapi.com/?apikey=caa4fbc9&t=it");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
static async Task<Film> GetFilmAsync(string path)
{
Film film = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
film = await response.Content.ReadAsAsync<Film>();
}
return film;
}
}
遗憾的是,它并没有做太多,我也不知道下一步该做什么。所以正如我所说,这应该从网络 api(http://www.omdbapi.com/) 读取数据。然后它必须将选定的项目放入 class 中,然后我可以用它来组成表格。
public class Film
{
public string Title { get; set; }
public string Released { get; set; }
public string Runtime { get; set; }
public string Genre { get; set; }
public string Director { get; set; }
public string Actors { get; set; }
public string Plot { get; set; }
public string Language { get; set; }
public string imdbRating { get; set; }
public string totalSeasons { get; set; }
public string Type { get; set; }
}
希望大家帮帮忙!不胜感激!
我对你的代码做了一些修改,它似乎运行良好:
async void Main()
{
await InitClient();
// Pass the file title to the API
var result = await GetFilmAsync("it");
client.CancelPendingRequests();
client.Dispose();
}
// Changed name to be more meaningful
static async Task InitClient()
{
// Remove the part with your key and the film title from the general initialization
client.BaseAddress = new Uri("http://www.omdbapi.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
static async Task<Film> GetFilmAsync(string title)
{
Film film = null;
// Compose the path adding the key and the film title
string path = "?apikey=caa4fbc9&t=" + title;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
string data = await response.Content.ReadAsStringAsync();
// This is the key point, the API returns a JSON string
// You need to convert it to your Film object.
// In this example I have used the very useful JSon.NET library
// that you can easily install with NuGet.
film = JsonConvert.DeserializeObject<Film>(data);
}
return film;
}