Junior — Middle
How can requests be sent using the HTTP protocol?
sobes.tech AI
Answer from AI
In C#, the HttpClient class is most commonly used for sending HTTP requests. The main methods of sending requests are:
- GET — retrieving data from the server.
- POST — sending data to the server.
- PUT — updating data.
- DELETE — deleting data.
Example of sending a GET request:
using System.Net.Http;
using System.Threading.Tasks;
HttpClient client = new HttpClient();
string url = "https://api.example.com/data";
HttpResponseMessage response = await client.GetAsync(url);
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
For POST requests, you can use the PostAsync method, passing the content as HttpContent.