Sobes.tech
Junior — Middle

What methods or tools have you used to request data from servers via the HTTP protocol?

sobes.tech AI

Answer from AI

In C#, the HttpClient class from the System.Net.Http namespace is most commonly used for making HTTP requests. It allows sending GET, POST, and other types of requests, as well as receiving responses asynchronously.

Example of a simple GET request:

using System.Net.Http;
using System.Threading.Tasks;

public async Task<string> GetDataAsync(string url)
{
    using HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync(url);
    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    return responseBody;
}

You can also use WebClient (deprecated) or the lower-level HttpWebRequest, but HttpClient is the recommended and more modern approach.

For complex scenarios, libraries like RestSharp are often used, which simplify working with REST APIs.