Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Why I am getting a http request exception ?

DyaryDyary Member ✭✭

Hi . I am trying to execute this simple code but it fails and i getSystem.Net.Http.HttpRequestException: An error occurred while sending the request ?

The code simply declares an HttpCLient and then I use the GetStringMethod. the same code used to work. I don't know why it doesn't now.
I searched google and stackoverflow and couldn't get the answer.

Here is my .cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using FetchActor.Models;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.Collections;

namespace FetchActor
{
    public partial class MainPage : ContentPage
    {




        HttpClient connection = new HttpClient();
        MovieService movieService = new MovieService();




        public MainPage()
        {
            InitializeComponent();

        }




        private async void searchBarByActor_SearchButtonPressed(object sender, EventArgs e)
        {



            var _connection = await connection.GetStringAsync("https://api.themoviedb.org/3/search/keyword?api_key=ab3da1af35c55cc8b3780fbc9a2bf259&query=Charlize%20Theron&page=1");


    }
}

the code never goes past GetStringAsync and i get the error.
any ideas on what could this be please ?

Answers

  • DyaryDyary Member ✭✭

    the ANSWER : It seems the emulator wasn't get data from the connection. I turned the Wifi on and off and restarted the computer and the project. it still didn't work. it was showing connected but no internet.

    the solution was turning off mobile data in the emulator so that it could be connected only with wifi.

Sign In or Register to comment.