1创建http类请求类型的封装
异步的请求
public static Task<string> SendAsync(string url, HttpType type = HttpType.GET, string data = "", bool isJson = false)
{
return Task.Run(() =>
{
return Send(url, type, data, isJson);
});
}
同步GET
private void button1_Click(object sender, EventArgs e)
{
string s = Http.Send("http://192.168.113.74:3000/All");
string s1 = Http.Send("http://192.168.113.74:3000/getTest?type=2");
Console.WriteLine(s);
this.label1.Text = s1;
}
同步post 字符串拼接
private void button2_Click(object sender, EventArgs e)
{
string s = Http.Send("http://192.168.113.74:3000/register", Http.HttpType.POST,"name=小玲&psw=1236796966",false);
this.label1.Text = s;
}
同步post2 JSON格
private void button3_Click(object sender, EventArgs e)
{
string s = Http.Send("http://192.168.113.74:3000/register", Http.HttpType.POST, "{\"name\":\"阿玲\",\"psw\":123456}",true);
this.label1.Text = s;
}
异步GET
private async void button6_Click(object sender, EventArgs e)
{
string s = await Http.SendAsync("http://192.168.113.74:3000/login?name=10001&psw=123456");
this.label1.Text= s;
}
异步post1 字符拼接
private async void button5_Click(object sender, EventArgs e)
{
string s = await Http.SendAsync("http://192.168.113.74:3000/register", Http.HttpType.POST, "name=艮奴盖地虎&psw=12345678", false);
this.label1.Text = s;
}
异步post2 JSON格式
private async void button4_Click(object sender, EventArgs e)
{
string s = await Http.SendAsync("http://192.168.113.74:3000/register", Http.HttpType.POST,"{\"name\":\"叮铃\",\"psw\":123456}",true);
this.label1.Text = s;
}