Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace testhttpclient
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string result = MakeCall().Result;
  15.             Console.WriteLine(result);
  16.             Console.ReadLine();
  17.         }
  18.  
  19.         public static async Task<string> MakeCall()
  20.         {
  21.             string result = String.Empty;
  22.             HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://www.example.com/"); //"http://www.icanhazip.com/");
  23.  
  24.             using (HttpClient httpClient = new HttpClient())
  25.             {
  26.                 using (HttpResponseMessage response = await httpClient.SendAsync(request))
  27.                 {
  28.                     if (response.IsSuccessStatusCode == true) // Takes approximately 22-23 seconds to reach this line
  29.                     {
  30.                         result = await response.Content.ReadAsStringAsync();  // Returns result quickly at this point
  31.                     }
  32.                     else
  33.                     {
  34.                         result = "Failure";
  35.                     }
  36.                 }
  37.             }
  38.             return result;
  39.  
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement