HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync("https://api.example.com/resource"); string responseBody = await response.Content.ReadAsStringAsync();
HttpClient client = new HttpClient(); string postData = "{ \"name\": \"John\", \"age\": 30 }"; HttpContent content = new StringContent(postData, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("https://api.example.com/resource", content); string responseBody = await response.Content.ReadAsStringAsync();
HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Authorization", "Bearer myToken"); HttpResponseMessage response = await client.GetAsync("https://api.example.com/resource"); string responseBody = await response.Content.ReadAsStringAsync();This code sets the "Authorization" header in the HttpClient instance and sends a GET request with the header included. The package library for HttpClient is "System.Net.Http".