Advertisement
Shell_Casing

calling API

Jul 29th, 2019
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using System.Net;
  5. using System.Net.Http;
  6.  
  7.  
  8. namespace HTTP_Test
  9. {
  10.     class program
  11.     {
  12.         static void Main()
  13.         {
  14.             Task t = new Task(HTTP_GET);
  15.             t.Start();
  16.             Console.ReadLine();
  17.         }
  18.  
  19.         static async void HTTP_GET()
  20.         {
  21.             var TARGETURL = "https://dev.azure.com/myOrganization/_apis/projects";
  22.  
  23.            
  24.             Console.WriteLine("GET " + TARGETURL);
  25.  
  26.             HttpClient client = new HttpClient();
  27.  
  28.             var byteArray = Encoding.ASCII.GetBytes("api-token:ejgm6xtauplqrsei3bqa");
  29.             client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
  30.  
  31.             HttpResponseMessage response = await client.GetAsync(TARGETURL);
  32.             HttpContent content = response.Content;
  33.  
  34.             // ... Check Status Code                                
  35.             Console.WriteLine("Response status code: " + (int)response.StatusCode);
  36.  
  37.             // ... Read the string.
  38.             string result = await content.ReadAsStringAsync();
  39.  
  40.             // ... Display the result.
  41.             if (result != null)
  42.             {
  43.                 Console.WriteLine(result);
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement