Skip to content

Instantly share code, notes, and snippets.

@WrackedFella
Last active September 3, 2018 15:57
Show Gist options
  • Save WrackedFella/9d48a7f4be119ec933fd253c9fc006bb to your computer and use it in GitHub Desktop.
Save WrackedFella/9d48a7f4be119ec933fd253c9fc006bb to your computer and use it in GitHub Desktop.
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace IntegrationTest
{
internal class BasicRequestWithHttpClient
{
private const string Username = "";
private const string Password = "";
private const string ResourceUrl = "";
private static void Main()
{
var response = GetResponse();
Console.WriteLine(response);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
private static string GetResponse()
{
using (var client = GetClient(Username, Password))
{
var task = client.GetStringAsync(ResourceUrl);
task.Wait();
return task.Result;
}
}
private static HttpClient GetClient(string username, string password)
{
var param = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
var authValue = new AuthenticationHeaderValue("Basic", param);
var client = new HttpClient
{
DefaultRequestHeaders = { Authorization = authValue }
//Set some other client defaults like timeout / BaseAddress
};
return client;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment