C# HTTP Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To make an HTTP GET web request to a URL in C#, you can use the HttpClient
class from the System.Net.Http
namespace. Here's an example:
using System.Net.Http; using System.Threading.Tasks; public async Task<string> GetResponseAsync(string url) { using (var client = new HttpClient()) { var response = await client.GetAsync(url); return await response.Content.ReadAsStringAsync(); } }
This code defines a method called GetResponseAsync
that takes a URL as input and returns a Task<string>
that will contain the response content.
To make an asynchronous HTTP GET request to a URL in C#, you can use the HttpClient.GetAsync
method, which returns a Task<HttpResponseMessage>
that you can await. Here's an example:
using System.Net.Http; using System.Threading.Tasks; public async Task<string> GetResponseAsync(string url) { using (var client = new HttpClient()) { var response = await client.GetAsync(url); return await response.Content.ReadAsStringAsync(); } }
This code is the same as the previous example, but it uses the await
keyword to asynchronously wait for the response to be returned.
C# HttpClient GET example:
HttpClient
.using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); // Process content } }
C# HTTPClient send GET request:
HttpClient
.HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync("https://api.example.com/data");
GET request in C# using WebClient:
WebClient
.using (WebClient client = new WebClient()) { string result = client.DownloadString("https://api.example.com/data"); // Process result }
Asynchronous HTTP GET request in C#:
using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); // Process content } }
Query parameters in C# HTTP GET request:
string apiUrl = "https://api.example.com/data"; string queryString = "?param1=value1¶m2=value2"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(apiUrl + queryString); // Process response }
REST API GET request in C#:
using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync("https://api.example.com/api/resource"); // Process response }
Deserialize JSON from GET request in C#:
using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); if (response.IsSuccessStatusCode) { string jsonContent = await response.Content.ReadAsStringAsync(); var data = JsonConvert.DeserializeObject<MyModel>(jsonContent); // Process data } }
HTTP GET request with headers in C#:
using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer your_access_token"); HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); // Process response }
C# HTTP GET request timeout:
using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(10); // Set timeout to 10 seconds HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); // Process response }
Handling redirects in C# HTTP GET request:
using (HttpClientHandler handler = new HttpClientHandler()) using (HttpClient client = new HttpClient(handler)) { handler.AllowAutoRedirect = true; HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); // Process response }
C# HTTP GET request with authentication:
using (HttpClient client = new HttpClient()) { var credentials = new NetworkCredential("username", "password"); client.DefaultRequestHeaders.Authorization = new CredentialCache { { new Uri("https://api.example.com"), "Basic", credentials } }; HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); // Process response }
URL encoding in C# HTTP GET request:
string parameterValue = "value with spaces"; string encodedValue = Uri.EscapeDataString(parameterValue); string apiUrl = $"https://api.example.com/data?param={encodedValue}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(apiUrl); // Process response }
C# HTTP GET request error handling:
using (HttpClient client = new HttpClient()) { try { HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); response.EnsureSuccessStatusCode(); // Throws exception for non-success status codes // Process response } catch (HttpRequestException ex) { // Handle exception } }
HTTP GET request with cancellation in C#:
using (CancellationTokenSource cts = new CancellationTokenSource()) using (HttpClient client = new HttpClient()) { cts.CancelAfter(TimeSpan.FromSeconds(5)); // Cancel after 5 seconds try { HttpResponseMessage response = await client.GetAsync("https://api.example.com/data", cts.Token); // Process response } catch (TaskCanceledException ex) { // Handle cancellation } }