C# HTTP Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To set the Keep-Alive
header to false
in an HttpClient
request, you can set the Connection
header to "close"
. Here is an example:
using System.Net.Http; var client = new HttpClient(); client.DefaultRequestHeaders.ConnectionClose = true; HttpResponseMessage response = await client.GetAsync("http://example.com");
To send content with an HttpClient
request, you can use the HttpContent
class. Here is an example:
using System.Net.Http; var client = new HttpClient(); var content = new StringContent("Hello, world!"); HttpResponseMessage response = await client.PostAsync("http://example.com", content);
To set a cookie on an HttpClient
request, you can use the Cookies
property of the HttpClientHandler
class. Here is an example:
using System.Net; using System.Net.Http; var handler = new HttpClientHandler(); handler.CookieContainer = new CookieContainer(); handler.CookieContainer.Add(new Uri("http://example.com"), new Cookie("name", "value")); var client = new HttpClient(handler); HttpResponseMessage response = await client.GetAsync("http://example.com");
In this example, a new CookieContainer
is created and a new Cookie
object is added to it. The HttpClientHandler
is then created with the CookieContainer
and used to create the HttpClient
. The HttpClient
automatically includes the cookie in the request.
C# HttpClient example:
HttpClient
for making HTTP requests.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 } }
Making HTTP requests in C#:
HttpWebRequest
for simplicity)HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.example.com/data"); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { // Process response }
C# HttpClient authentication:
HttpClient
requests.HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "your_access_token");
HTTP GET and POST in C#:
HttpClient
for both methods)using (HttpClient client = new HttpClient()) { HttpResponseMessage getResponse = await client.GetAsync("https://api.example.com/data"); HttpResponseMessage postResponse = await client.PostAsync("https://api.example.com/data", new StringContent("data")); }
Using HttpClient with JSON in C#:
HttpClient
.HttpClient client = new HttpClient(); string jsonData = JsonConvert.SerializeObject(new { key = "value" }); StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("https://api.example.com/data", content);
C# HttpClient error handling:
HttpClient
.try { HttpResponseMessage response = await client.GetAsync("https://api.example.com/data"); response.EnsureSuccessStatusCode(); // Throws exception for non-success status codes } catch (HttpRequestException ex) { // Handle exception }
HttpClient vs WebClient in C#:
HttpClient
and WebClient
for making HTTP requests.WebClient
)using (WebClient client = new WebClient()) { string result = client.DownloadString("https://api.example.com/data"); // Process result }
Asynchronous HTTP requests in C#:
HttpClient
.async Task GetDataAsync() { 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 and receive data:
HttpClient
.HttpClient client = new HttpClient(); string data = "Some data to send"; HttpResponseMessage response = await client.PostAsync("https://api.example.com/data", new StringContent(data));
C# HttpClient timeout handling:
HttpClient
.HttpClient client = new HttpClient(); client.Timeout = TimeSpan.FromSeconds(30); // Set timeout to 30 seconds