Xamarin forms Get Request Not working for HTTP and HTTPS

Viewed 1507

I have created an application in Xamarin forms with Visual studio 2017 Professional and Visual studio 2019 cumulative addition.

But i am able to hit post request in both protocol http and https, but i am not able to hit Get request in both protocol.

using (var client = new HttpClient())
        {
            try
           {
                
                var response1 = await client.GetStringAsync("https://reqres.in/api/users?page=2");
                
            }
            catch (WebException ex)
            {
                
            }
        }

Xamrin.form version both 3.5 stable version and 4.6 stable version.

Not able to hit get request.

1 Answers

I need to change my code as below, after it everything is working fine.

 client = new HttpClient();
        client.DefaultRequestHeaders.Add("Accept", "application/json");
        client.DefaultRequestHeaders.Add("User-Agent", "HelloWorld.MyApp");
        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, targetUri);
        HttpResponseMessage response = await client.SendAsync(requestMessage).ConfigureAwait(false);
Related