1
 async void GetLieu(string place)
           {

            ShowProgressDialogue("Récupération des données...");

            string MonURL = "https://hubeau.eaufrance.fr/api/v1/hydrometrie/referentiel/stations";
            string url = MonURL + "?code_departement=" + place;
           
            var handler = new HttpClientHandler();
            HttpClient client = new HttpClient(handler);
            string result = await client.GetStringAsync(url);

            Console.WriteLine(result);
            var resultObjet = JObject.Parse(result);

hello, I have a problem that I can't solve. I can't adapt my code to prevent it crashing at the level of :

await client.GetStringAsync (url);

which would have a solution without modifying : async void GetLieu (string place)) ?

I am taker of any information. thank you in advance.

Progman
  • 16,827
  • 6
  • 33
  • 48
michael
  • 25
  • 7
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the full source code you have as a [mcve], which can be compiled and tested by others. Include the error/exception message you get and/or the problem you are getting to your question. – Progman Dec 12 '20 at 22:01
  • Is the problem solved? – Useme Alehosaini Dec 12 '20 at 23:04
  • my problem change "void 'to' Task 'I have a problem with my" GetLieu (place) "which no longer works. I am new to this language but I perceive ... – michael Dec 13 '20 at 15:26
  • @michael , my answer has been updated according to your last comment – Useme Alehosaini Dec 14 '20 at 20:51

1 Answers1

0

Reference your last comment:

Try this code:

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace StackOverflowWebClientQuestionAnswer
{
    public class ExternalService
    {
        public async Task<string> DownloadHomepage(string place)
        {
            string MonURL = "https://hubeau.eaufrance.fr/api/v1/hydrometrie/referentiel/stations";
            string url = MonURL + "?code_departement=" + place;

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) " +
                                                    "AppleWebKit/537.36 (KHTML, like Gecko) " +
                                                    "Chrome/58.0.3029.110 Safari/537.36");

                var resultat = await httpClient.GetStringAsync(new Uri(url));

                return resultat;
            }
        }
    }
}

how to call it (example)?!

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace StackOverflowWebClientQuestionAnswer.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class Resultat : ControllerBase
    {
        [HttpGet]
        public async Task<IActionResult> GetResultat()
        {
            ExternalService external = new ExternalService();

            string resultat = await external.DownloadHomepage("971");

            return Ok(resultat);
        }
    }
}

Results:

Result

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26