Make an HTTP GET request in C#
This post will discuss how to make an HTTP GET request in C#.
1. Using WebClient.DownloadString()
method
The System.Net.WebClient
class provides several utility methods for sending data and receiving data from a URI resource. You can use the WebClient.DownloadString() method to download a resource as a string. The following example illustrates the usage of this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Net; public class Example { public static string Get(string url) { var web = new WebClient(); return web.DownloadString(url); } public static void Main() { String url = "https://reqres.in/api/users?page=1"; String content = Get(url); Console.WriteLine(content); } } |
Note that the WebClient.DownloadString()
method blocks the calling thread while downloading the resource. To download a resource asynchronously without waiting for the server’s response, consider using the WebClient.DownloadStringAsync()
method.
2. Using HttpWebRequest
Class
Alternatively, you can use the System.Net.HttpWebRequest
class, which is an HTTP-specific implementation of the WebRequest
class. For more information on this class, see the official documentation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Net; public class Example { public static string Get(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } public static void Main() { String url = "https://reqres.in/api/users?page=1"; String content = Get(url); Console.WriteLine(content); } } |
3. Using HttpClient
Class
The HttpWebRequest
class is not recommended anymore. It is preferable to use the System.Net.Http.HttpClient
class instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Net; public class Example { private static HttpClient client = null; public static string Get(string url) { if (client == null) { HttpClientHandler handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; client = new HttpClient(handler); } client.BaseAddress = new Uri(url); HttpResponseMessage response = client.GetAsync("").Result; response.EnsureSuccessStatusCode(); return response.Content.ReadAsStringAsync().Result; } public static void Main() { String url = "https://reqres.in/api/users"; String content = Get(url); Console.WriteLine(content); } } |
That’s all about making an HTTP GET request in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)