Instantly share code, notes, and snippets.

Embed
What would you like to do?
HttpClient.cs Authentication Error?
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleTestApp1
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
SeedDataInline2().GetAwaiter().GetResult();
Console.ReadLine();
}
public static async Task SeedDataInline2()
{
// Arrange
var client = new HttpClient();
//client.DefaultRequestHeaders.Clear();
//client.BaseAddress = new Uri(settings.Url);
var login = "abc";
var password = "def";
var requestUri = "https://blah/blah/blah/1";
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{login}:{password}"));
//client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
request.Version = Version.Parse("1.1");
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Act
var response = await client.SendAsync(request);
// Assert
Console.WriteLine(response.StatusCode);
response.EnsureSuccessStatusCode();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment