Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Xamarin Forms App Calling Web API Pin

Xamarin Forms App Calling Web API Pin

Scheduled Pinned Locked Moved Android
jsoncsharpasp-netmobiledotnet
10 Posts 2 Posters 15 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    'm learning Xamaring Forms. I want to test connecting to a Web API. I have a an Asp.Net Framework Web API running and I can connect to to it. However, in my Xamarin Forms page, when I call it, the call to GetAsync never returns:

    public class MainPageViewModel : _ViewModelBase
    {
    private string url = "https://localhost:44340/api/Test/GetAll";
    public List Items { get; set; }
    public MainPageViewModel()
    {
    GetData();
    }
    private async Task GetData()
    {
    try
    {
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync(url); // <====== NEVER RETURNS

            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();
                Items = JsonConvert.DeserializeObject\>(content);
            }
        }
        catch (Exception e)
        {
            throw;
        }
    }
    

    }

    I can paste the URL into a browser and see the JSON results. If the WebAPI is NOT running the GetAsync throws. Anyone have any thoughts on this?

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      'm learning Xamaring Forms. I want to test connecting to a Web API. I have a an Asp.Net Framework Web API running and I can connect to to it. However, in my Xamarin Forms page, when I call it, the call to GetAsync never returns:

      public class MainPageViewModel : _ViewModelBase
      {
      private string url = "https://localhost:44340/api/Test/GetAll";
      public List Items { get; set; }
      public MainPageViewModel()
      {
      GetData();
      }
      private async Task GetData()
      {
      try
      {
      HttpClient client = new HttpClient();
      HttpResponseMessage response = await client.GetAsync(url); // <====== NEVER RETURNS

              if (response.IsSuccessStatusCode)
              {
                  string content = await response.Content.ReadAsStringAsync();
                  Items = JsonConvert.DeserializeObject\>(content);
              }
          }
          catch (Exception e)
          {
              throw;
          }
      }
      

      }

      I can paste the URL into a browser and see the JSON results. If the WebAPI is NOT running the GetAsync throws. Anyone have any thoughts on this?

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      It sounds like you have a deadlock somewhere. Try adding .ConfigureAwait(false) to the GetAsync call.

      private async Task GetData()
      {
      try
      {
      HttpClient client = new HttpClient();
      HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);

          if (response.IsSuccessStatusCode)
          {
              string content = await response.Content.ReadAsStringAsync();
              Items = JsonConvert.DeserializeObject<List<TestDataEntity>>(content);
          }
      }
      catch (Exception e)
      {
          throw;
      }
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        It sounds like you have a deadlock somewhere. Try adding .ConfigureAwait(false) to the GetAsync call.

        private async Task GetData()
        {
        try
        {
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();
                Items = JsonConvert.DeserializeObject<List<TestDataEntity>>(content);
            }
        }
        catch (Exception e)
        {
            throw;
        }
        

        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Well that helped, a bit. Again, I'm just trying to create a small, simple xamarin forms example that connects to a web api. I've read these: [Consume a RESTful Web Service - Xamarin | Microsoft Docs](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest) [6 Steps to Consume ASP.Net Core Web API in Xamarin | Syncfusion Blogs](https://www.syncfusion.com/blogs/post/consume-asp-net-core-web-api-in-xamarin.aspx) Here's what I have now:

        private async Task GetData()
        {
        //string url = $"https://localhost:44340/api/Test/GetAll";
        string url = $"https://127.0.0.1:44340/api/Test/GetAll";
        try
        {
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);
        if (response.IsSuccessStatusCode)
        {
        string content = await response.Content.ReadAsStringAsync();
        Items = JsonConvert.DeserializeObject>(content);
        }
        }
        catch (Exception e)
        {
        throw;
        }
        }

        My browser opens and shows the commented out url (https:/localhost:44340). Yet the call to GetAsync throws "{System.Net.WebException: Failed to connect to localhost/127.0.0.1:44340". When I try that URL I get the same exception.

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        Richard DeemingR 1 Reply Last reply
        0
        • K Kevin Marois

          Well that helped, a bit. Again, I'm just trying to create a small, simple xamarin forms example that connects to a web api. I've read these: [Consume a RESTful Web Service - Xamarin | Microsoft Docs](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest) [6 Steps to Consume ASP.Net Core Web API in Xamarin | Syncfusion Blogs](https://www.syncfusion.com/blogs/post/consume-asp-net-core-web-api-in-xamarin.aspx) Here's what I have now:

          private async Task GetData()
          {
          //string url = $"https://localhost:44340/api/Test/GetAll";
          string url = $"https://127.0.0.1:44340/api/Test/GetAll";
          try
          {
          HttpClient client = new HttpClient();
          HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);
          if (response.IsSuccessStatusCode)
          {
          string content = await response.Content.ReadAsStringAsync();
          Items = JsonConvert.DeserializeObject>(content);
          }
          }
          catch (Exception e)
          {
          throw;
          }
          }

          My browser opens and shows the commented out url (https:/localhost:44340). Yet the call to GetAsync throws "{System.Net.WebException: Failed to connect to localhost/127.0.0.1:44340". When I try that URL I get the same exception.

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Is the WebAPI project running? That looks like an IIS Express URL, which means you'll need to run the API project from Visual Studio for the URL to work. Also, if you're using a self-signed certificate and you haven't added it to your local PC's trust store, you'll need some extra code to ignore the certificate error: c# - Allowing Untrusted SSL Certificates with HttpClient - Stack Overflow[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          K 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Is the WebAPI project running? That looks like an IIS Express URL, which means you'll need to run the API project from Visual Studio for the URL to work. Also, if you're using a self-signed certificate and you haven't added it to your local PC's trust store, you'll need some extra code to ignore the certificate error: c# - Allowing Untrusted SSL Certificates with HttpClient - Stack Overflow[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            Yes, the API is running. In the browser I can type localhost:44340/api/test/GetAll and it returns json data from my repo. localhost:44340 is what VS gives me when I run the app. So I'm trying that same URL in the Android app. Yet the exception shows the other IP address

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            Richard DeemingR 1 Reply Last reply
            0
            • K Kevin Marois

              Yes, the API is running. In the browser I can type localhost:44340/api/test/GetAll and it returns json data from my repo. localhost:44340 is what VS gives me when I run the app. So I'm trying that same URL in the Android app. Yet the exception shows the other IP address

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              localhost is an alias for 127.0.0.1, so it's resolving to the correct address. Did you add the self-signed certificate to the local computer's trusted certificates store?


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              K 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                localhost is an alias for 127.0.0.1, so it's resolving to the correct address. Did you add the self-signed certificate to the local computer's trusted certificates store?


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                K Offline
                K Offline
                Kevin Marois
                wrote on last edited by
                #7

                Richard Deeming wrote:

                Did you add the self-signed certificate to the local computer's trusted certificates store?

                What cert are you referring to?

                If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                Richard DeemingR 1 Reply Last reply
                0
                • K Kevin Marois

                  Richard Deeming wrote:

                  Did you add the self-signed certificate to the local computer's trusted certificates store?

                  What cert are you referring to?

                  If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  IIS Express issues a self-signed certificate to serve your application over HTTPS instead of HTTP. When you open the site in a browser, you should see a warning that the certificate is untrusted.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  K 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    IIS Express issues a self-signed certificate to serve your application over HTTPS instead of HTTP. When you open the site in a browser, you should see a warning that the certificate is untrusted.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    K Offline
                    K Offline
                    Kevin Marois
                    wrote on last edited by
                    #9

                    I'm running from VS. When I start the app Chrome opens and I see JSON returned from my DAL. But no cert warnings.

                    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                    Richard DeemingR 1 Reply Last reply
                    0
                    • K Kevin Marois

                      I'm running from VS. When I start the app Chrome opens and I see JSON returned from my DAL. But no cert warnings.

                      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      It sounds like you've added the certificate to Chrome's certificate store, or enabled chrome://flags/#allow-insecure-localhost to bypass the certificate warning.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups