我正在尝试以 Windows 形式制作一个程序,该程序可以通过 web api 搜索电影和系列。然后程序需要在表单上显示所有相关信息。
我被困在让程序从 web api 中读取。经过大量搜索,我决定把它放在这里。
我有一些主要基于这篇文章的代码:https ://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-客户
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;
}
}
可悲的是,它没有多大作用,我不知道下一步该做什么。所以正如我所说,这应该从 web api( http://www.omdbapi.com/)读取数据。然后它必须将选定的项目放入一个类中,然后我可以用它来组成表格。
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; }
}
我希望任何人都可以提供帮助!任何事情都值得赞赏!