Der HttpClient-Aufruf gibt keine Antwort zurück [Duplikat]C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Der HttpClient-Aufruf gibt keine Antwort zurück [Duplikat]

Post by Anonymous »

Im folgenden (vereinfachten) Code scheint der Aufruf von GetLocation einfach im Debugger hängen zu bleiben und gelangt nie zu dem folgenden „mehr Code“. Was mache ich falsch?

Code: Select all

namespace abc
{
public partial class Form1 form
{
public Form1
{
...
}

private OnSomeButtonClicked
{
/* some code */

Task taskRes = GetLocation(ap.Latitude, ap.Longitude);

string[] loc = taskRes.Result;

/* some more code */
}

private async Task GetLocation(double lat, double lon)
{
string url = $"https://api.geoapify.com/v1/geocode/reverse?lat={lat}&lon={lon}&format=json&apiKey={apiKey}";
string result;

using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);

if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"Geoapify client.GetAsync request failed with status code {response.StatusCode}");
}

result = await response.Content.ReadAsStringAsync();
}

return res;
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post