C# HTTP Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To make an HTTP GET request with parameters in C#, you can use the HttpClient
class from the System.Net.Http
namespace. Here's an example:
using System; using System.Net.Http; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { var httpClient = new HttpClient(); var queryParameters = new Dictionary<string, string> { {"param1", "value1"}, {"param2", "value2"} }; var queryString = string.Join("&", queryParameters .Select(x => $"{Uri.EscapeDataString(x.Key)}={Uri.EscapeDataString(x.Value)}")); var url = $"https://example.com/path?{queryString}"; var response = await httpClient.GetAsync(url); var content = await response.Content.ReadAsStringAsync(); Console.WriteLine(content); } }
In this example, we create a Dictionary
object to store the query parameters we want to send with the request. We then create a queryString
variable by joining the key-value pairs in the dictionary with the &
character, and we use Uri.EscapeDataString()
to escape any special characters in the parameter values.
Next, we construct the full URL by concatenating the base URL and the query string. We then make an asynchronous GET request using the HttpClient.GetAsync()
method and passing in the URL.
Finally, we retrieve the response content using the HttpResponseMessage.Content
property and call the ReadAsStringAsync()
method to convert the content to a string.
To build the query string for an HTTP GET request using the HttpClient
class, you can use the same approach as shown in the example above.
C# HttpClient GET request with query parameters:
HttpClient
.using (HttpClient client = new HttpClient()) { var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" } }; string queryString = string.Join("&", parameters.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}")); HttpResponseMessage response = await client.GetAsync($"https://api.example.com/data?{queryString}"); // Process response }
Passing parameters in C# WebClient GET request:
WebClient
.using (WebClient client = new WebClient()) { var parameters = new NameValueCollection { { "param1", "value1" }, { "param2", "value2" } }; string result = client.DownloadString("https://api.example.com/data?" + string.Join("&", parameters.AllKeys.Select(key => $"{key}={Uri.EscapeDataString(parameters[key])}"))); // Process result }
REST API GET request with parameters in C#:
using (HttpClient client = new HttpClient()) { var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" } }; string queryString = string.Join("&", parameters.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}")); HttpResponseMessage response = await client.GetAsync($"https://api.example.com/api/resource?{queryString}"); // Process response }
Building query string for GET request in C#:
var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" } }; string queryString = string.Join("&", parameters.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}")); string apiUrl = $"https://api.example.com/data?{queryString}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(apiUrl); // Process response }
Adding headers to GET request in C# with parameters:
using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer your_access_token"); var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" } }; string queryString = string.Join("&", parameters.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}")); HttpResponseMessage response = await client.GetAsync($"https://api.example.com/data?{queryString}"); // Process response }
Query parameter serialization in C# HTTP GET:
var parameters = new { param1 = "value1", param2 = "value2" }; string queryString = string.Join("&", parameters.GetType().GetProperties().Select(p => $"{p.Name}={Uri.EscapeDataString(p.GetValue(parameters)?.ToString())}")); string apiUrl = $"https://api.example.com/data?{queryString}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(apiUrl); // Process response }
C# HTTP GET request with model parameters:
var parameters = new MyModel { Param1 = "value1", Param2 = "value2" }; string queryString = string.Join("&", parameters.GetType().GetProperties().Select(p => $"{p.Name}={Uri.EscapeDataString(p.GetValue(parameters)?.ToString())}")); string apiUrl = $"https://api.example.com/data?{queryString}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(apiUrl); // Process response }
C# HTTP GET request with authentication and parameters:
using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer your_access_token"); var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" } }; string queryString = string.Join("&", parameters.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}")); HttpResponseMessage response = await client.GetAsync($"https://api.example.com/data?{queryString}"); // Process response }
Handling null or optional parameters in C# HTTP GET:
var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", null }, // Optional or nullable parameter { "param3", "value3" } }; string queryString = string.Join("&", parameters.Where(p => p.Value != null).Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}")); string apiUrl = $"https://api.example.com/data?{queryString}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(apiUrl); // Process response }