get请求可以使用GetAsync方法
Post请求 使用PostAsync,
delete请求 使用DeleteAsync
Put请求 使用PutAsync
请求的完整写法
HttpResponseMessage res = await new HttpClient().SendAsync(reg);
string data = await res.Content.ReadAsStringAsync();
Console.WriteLine(data);
MessageBox.Show(res.StatusCode + "");// 状态码
2 发送请求
HttpRequestMessage reg = new HttpRequestMessage()
{
Method = HttpMethod.Head, // 设置不同求情类型
RequestUri = new Uri("http://127.0.0.1:3000/Shop")
};
获取响应的响应头
HttpResponseMessage res = await new HttpClient().SendAsync(reg);
string data = await res.Content.ReadAsStringAsync();
Console.WriteLine(data);
MessageBox.Show(res.StatusCode + "");// 状态码
// 获取响应的响应头
HttpResponseMessage res1 = await new HttpClient().GetAsync("https://192.168.113.74:3000/Student");
// 获取响应的响应头
HttpResponseHeaders headers = res1.Headers;
foreach (var item in headers)
{
// item.Key 相应头键
// item.Value 响应得到值是数组的类型
Console.WriteLine("响应头的属性" + item.Key);
foreach (var item1 in item.Value)
{
await Console.Out.WriteLineAsync("响应头的属性" + item1);
}
}