c# request using http client
Mon Jun 19 2023 22:30:41 GMT+0000 (Coordinated Universal Time)
Saved by
@kursatoguz
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);
}
}
}
content_copyCOPY
Comments