1

I've created a Web API in 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, ProductName, Description and Price, I did 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, I get the following error message in break mode:

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

public class DataService
{

    public async Task<List<Product>> GetProductAsync(string ProductName)
    {

        using (var client = new HttpClient())
        {

            string url = "http://ProductsAPI.hostname.com/api";

            try
            {

               var uri = url + "/" + ProductName.ToString();
               HttpResponseMessage response = await client.GetAsync(uri);
               var ProductJsonString = awaitresponse.Content.ReadAsStringAsync();
               var Product = JsonConvert.DeserializeObject<List<Product>>(ProductJsonString);

               return Product;

            }

            catch (Exception ex)

            {
                throw ex;
            }

        }

    }

}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • What is the status code of the request? https://stackoverflow.com/questions/22217619/how-do-i-get-statuscode-from-httprequestexception – Hichame Yessou Jul 05 '18 at 13:52
  • The timeout is not the real error, it is an error generated by Visual Studio. Please check the error details. – Gerald Versluis Jul 05 '18 at 14:10
  • I received this message Unhandled Exception: System.Net.Http.HttpRequestException: An error occurred while sending the request – Luiz Fernando Jul 05 '18 at 14:20
  • Do you have any proxy limitations or firewall port blocks? – RedNet Jul 05 '18 at 14:40
  • Might try using Fiddler (https://www.telerik.com/fiddler) to look at the actual request going out and result coming back. – DaveG Jul 05 '18 at 15:09
  • It should be `await reponse.Content.ReadAsStringAsnyc()` (you're missing a space). Also, calling `ToString` on a `string` variable is a bit redundant. Otherwise, there's no way for us to determine what the issue is here. It all depends on how your Web API and application are set up. – Heretic Monkey Jul 05 '18 at 23:04
  • Are you trying on android or ios? For the latter you need to use an ssl connection or opt out from ATS in info.plist. Are you using the managed httpclient (mono)? – GiampaoloGabba Jul 05 '18 at 23:14

3 Answers3

0

Here's what I've used in the past:

public string GetAPIJsonAsync(string URL)
    {
        using (WebClient wc = new WebClient())
        {
            return wc.DownloadString(URL);
        }
    }

This would return the raw JSON to whoever called it, and I would then convert it to the desirable object.

RedNet
  • 416
  • 5
  • 12
0

If you increase the timeout of the HttpClient, does it return more information?

Also, try Refit It does all the work for you, including deserializing into json.

Curtis Shipley
  • 7,990
  • 1
  • 19
  • 28
0

This Works Perfectly for me

  public static async Task<List<BranchMasterModel>> GetBranchList(int city)
    {
        var client = new HttpClient(new NativeMessageHandler());

        client.BaseAddress = new Uri(UrlAdd);//("http://192.168.101.119:8475/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "AuthToken"));
        var result = await client.GetAsync("api/Master/V2/Branch/"+city);
        string branch = await result.Content.ReadAsStringAsync();
        var branches = JsonConvert.DeserializeObject<List<BranchMasterModel>>(branch);
        return branches;
    }
Ronak Shetiya
  • 960
  • 6
  • 27