On the browser, I could get data like this.(JSON format)
I want to perform HTTP requests and get
data on WinForm. How can I do to make it like the below picture?
I have referred to some relevant information. But I am confused how to start (like I should write code in Form1.cs or add new class, should I create model...)
How to make HTTP POST web request
How to return async HttpClient responses back to WinForm?
Can I use HttpClient Method? Thanks for answer and suggestion.
(New edit)
https://www.youtube.com/watch?v=PwH5sc-Q_Xk
I also learned from this video, But I got error message.
No MediaTypeFormatter is available to read an object of type 'IEnumerable`1' from content with media type 'text/html'.
My code
Form1.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net.Http;
using System.Net.Http.Formatting;
namespace _123
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
HttpClient clint = new HttpClient();
clint.BaseAddress = new Uri("http://localhost:8888/");
HttpResponseMessage response = clint.GetAsync("PersonList").Result;
var emp = response.Content.ReadAsAsync<IEnumerable<ImgList>>().Result;
dataGridView1.DataSource = emp;
}
}
}
ImgList.cs (Is this Model?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace _123
{
class ImgList
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
}