Consuming a WEB API in Visual Studio

Asked

Viewed 357 times

1

I created a WEB API on Asp.net that is hosted on a web server. This WEB API accesses a table in SQL Server where I have a table called Products with Id, name, description and Price, I ran the tests via POSTMAN and it is working correctly, but when I try to consume the method to bring a specific product via Xamarin application in visual studio

I get the following error message in interrupt mode

application in Interrupt mode

Unhandled Exception:

System.Net.Http.HttpRequestException: <Timeout exceeded getting exception details>

Below is my code to bring a specific product accessing the API on the server

public class DataService
{
    public async Task<List<Produto>> GetProductAsync(string NomeProduto)
    {
        using (var client = new HttpClient())
        {

            string url = "http://ProdutosAPI.servidor.com/api";

            try
            {
               var uri = url + "/" + NomeProduto.ToString();
               HttpResponseMessage response = await client.GetAsync(uri);
               var ProdutoJsonString = await response.Content.ReadAsStringAsync();
               var Produto = JsonConvert.DeserializeObject<List<Produto>>(ProdutoJsonString);
               return Produto;

            }
            catch (Exception ex)

            {
                throw ex;
            }

        }

    }

}
  • First translate your question into Portuguese because it is in stackoverflow em português

  • show! Now just wait until they help you. Success!!!

  • Which version of Visual Studio and which version of Xamarin?

  • Friend, if in POSTMAN works and in your device does not, check the internet permissions of the application. Source: Permissions in Xamarin

  • Hello, Where is your application running? You should check the API access from the device that runs the application. Try to access the API address via device browser / emulator and if you cannot check the network settings (firewall, proxy, topology etc.) of your service.

  • Visual studio 2017 - I’m getting this message Unhandled Exception: System.Net.Http.Httprequestexception: An error occurred while sending the request

Show 1 more comment

1 answer

1

Luiz, despite being an API and online, the server blocks requests from outside for security reasons.

For it to work in your global.asa file, try so.

private void EnableCrossDmainAjaxCall()
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                // HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }

        }

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.Cache.SetNoStore();
            EnableCrossDmainAjaxCall();
        }

If it doesn’t work, give it a read: https://stackoverflow.com/questions/6290053/setting-access-control-allow-origin-in-asp-net-mvc-simplest-possible-method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.