Created
May 22, 2018 19:54
-
-
Save jesslilly/ab303b4168880c3236aa9fe707494dd9 to your computer and use it in GitHub Desktop.
HttpClient.cs Authentication Error?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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