-1

I created http request with HttpClient class.I want to make GET request to some url. For example "http://www.google.com", or another url.

My application hosted in Amazon EC2 and use .Net Core 2.2.

    static HttpClient client = new HttpClient();

    private static async Task HttpClientRequestAsync(string url)
    {
        try
        {
            var response = await client.GetAsync(url);

            string responseJson = await response.Content.ReadAsStringAsync();

            Console.WriteLine(responseJson);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

I don't know why, but my request stuck in client.GetAsync. After that nothing happens, without any response or exceptions. It is very strangely, because I can see this behavior only on Amazon EC2 instance. In my local machine this request successfully run. When I use Postman or Chrome on EC2 machine and create the same request - I receive response. If i replace HttpClient to WebRequest - I have the same behavior.

Thank you in advance

Minimal reproducible example for Console App:

class Program
{
    static void Main(string[] args)
    {
        HttpClientRequestAsync().GetAwaiter().GetResult();

        Console.ReadLine();
    }

    static HttpClient client = new HttpClient();

    private static async Task HttpClientRequestAsync()
    {
        try
        {
            Console.WriteLine("Your url:");
            var url = Console.ReadLine();

            var response = await client.GetAsync(url);
            string responseJson = await response.Content.ReadAsStringAsync();

            Console.WriteLine(responseJson);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Dev123
  • 53
  • 1
  • 9

2 Answers2

0

Instead of

HttpClientRequestAsync().GetAwaiter().GetResult();

use this:

Task.Run(async () => await HttpClientRequestAsync());
-1

Disabling proxy when i created HttpClient helped me: UseProxy = false; Proxy = null;

Dev123
  • 53
  • 1
  • 9