是的,C#中的HttpRequestMessage類支持GET請求。可以通過設置HttpRequestMessage對象的Method屬性為HttpMethod.Get來發送GET請求。例如:
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com/data");
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("Request failed with status code: " + response.StatusCode);
}
}
}
}
在上面的示例中,我們創建了一個GET請求,指定了請求的URL為"https://api.example.com/data"。然后使用HttpClient發送該請求,并等待響應。如果響應成功,則將響應內容打印到控制臺上。