Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

httprequestmessage with authorization .net 5

//setup reusable http client
HttpClient client = new HttpClient();
Uri baseUri = new Uri(url);
client.BaseAddress = baseUri;
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.ConnectionClose = true;

//Post body content
var values = new List<KeyValuePair<string, string>>();
values.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
var content = new FormUrlEncodedContent(values);

var authenticationString = $"{clientId}:{clientSecret}";
var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));

var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/oauth2/token");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString);
requestMessage.Content = content;

//make the request
var task = client.SendAsync(requestMessage);
var response = task.Result;
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(responseBody);
Comment

PREVIOUS NEXT
Code Example
Csharp :: who is dani? game dev 
Csharp :: c# unary operators 
Csharp :: how to make a 2d character move in unity 2020 
Csharp :: c# wtssendmessage 
Csharp :: c# capitalize first letter of each word 
Csharp :: DrawImage resize to target size c# 
Csharp :: how can find github issue closed date 
Csharp :: Filter list contents with predicate (Lambda) 
Csharp :: textbox center align winform 
Csharp :: how to store more precise data then float c# 
Csharp :: c# ipaddress to integer 
Csharp :: c# generate random key with specified length 
Csharp :: @using System,System.Core 
Csharp :: satisfactory controller support 
Csharp :: unity manager.instance 
Csharp :: unity set terrain to image 
Csharp :: PUN 2 Network Transform View Jittery Movement 
Csharp :: tulpep notification window example c# 
Csharp :: how to pass object as test case in nunit c# 
Csharp :: f# set function how to ignore duplicates 
Csharp :: export xml 
Csharp :: how to get the askii code of a char in c# 
Csharp :: trigger checkbox combobox wpf 
Csharp :: how to add a round image unity 
Csharp :: enum extends dictionary c# 
Csharp :: Function delegate 
Csharp :: identity-1.us-south.iam.test.cloud.ibm.com:443 
Csharp :: c sharp while statement 
Csharp :: how to authorize token when consuming api in c# 
Csharp :: Smooth Sentences c# 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =