Well1AmNichtDiePerson
Established
C# Rest API Client sample code for login using httpclient (post method) given login credentials (API key, username, password)
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace RestApiClient
{
class Program
{
static async Task Main(string[] args)
{
string apiUrl = "https://api.example.com/login";
string apiKey = "YOUR_API_KEY";
string username = "USERNAME";
string password = "PASSWORD";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Api-Key", apiKey);
var loginData = new
{
username = username,
password = password
};
var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(loginData), Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(apiUrl, content);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine("Login successful! Response: " + responseContent);
}
else
{
Console.WriteLine("Login failed. Status code: " + response.StatusCode);
}
}
}
}
}
https://api.example.com/login with the actual API endpoint for login.YOUR_API_KEY, USERNAME, and PASSWORD with your actual API key, username, and password.Newtonsoft.Json package installed in your project for JSON serialization and deserialization.