using System; using System.Net.Http; namespace Example { class Program { static void Main(string[] args) { // Create an HttpClient instance. HttpClient client = new HttpClient(); // Set the request method. client.DefaultMethod = "GET"; // Set the request URI. Uri uri = new Uri("https://jsonplaceholder.typicode.com/posts"); // Make the request. HttpResponseMessage response = await client.GetAsync(uri); // Check the response status code. int statusCode = response.StatusCode; if (statusCode != HttpStatusCode.OK) { throw new Exception("The request failed with status code " + statusCode); } // Get the response content. string content = await response.Content.ReadAsStringAsync(); // Display the response content. Console.WriteLine(content); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter