0

I'm trying to make https webAPI call, specifically - Google Directions API. Putting the uri directly inside browser gives me the result that I want, so I'm 100% sure my uri is correct.

Now, how do I call the webapi inside my PCL? Using modernhttp and HttpClient now, but am open to whatever options there are out there.

private async Task<string> GetJsonObjFromUrl(string urlRoutes)
{
    HttpClient c = new HttpClient(new NativeMessageHandler());

    var resp = await c.SendAsync(new HttpRequestMessage(HttpMethod.Get, new Uri(urlRoutes)));

    if (resp.IsSuccessStatusCode)
    {
        var json = await resp.Content.ReadAsStringAsync();

        return json;
    }

    return null;
}

What am I doing wrong?

Edit: Just putting this here because this was driving me crazy whole night. Ends up the caller way, way above forgot to put await. The execution continues straight after and never returns to get the result. That's why I never got any results... :\

Farid
  • 872
  • 1
  • 13
  • 30
  • `What am I doing wrong?` Why do you think you're doing something wrong? Are you getting a compile-time error? An exception? A deadlock? An unexpected result? – Stephen Cleary Jan 23 '17 at 14:34
  • The code just don't go hit anywhere below client.SendAsync / GetStringAsync – Farid Jan 23 '17 at 14:52
  • @Farid try this one https://developer.xamarin.com/guides/cross-platform/transport-layer-security/ – XTL Jan 23 '17 at 17:01
  • check the following links : http://stackoverflow.com/questions/41680217/how-to-use-wcf-service-in-xamarin-forms-portable-class-library/41808780#41808780 – Mohamad Mahmoud Darwish Jan 23 '17 at 20:15

2 Answers2

1

The code just don't go hit anywhere below client.SendAsync / GetStringAsync

I suspect that further up your call stack, your code is calling Result / Wait / GetAwaiter().GetResult() on a task. If called from a UI thread, this will deadlock, as I explain on my blog.

The deadlock is caused by the async method attempting to resume on the UI context, but the UI thread is blocked waiting for the task to complete. Since the async method must complete in order to complete its task, there's a deadlock.

The proper fix is to replace that Result / Wait with await.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

In your PCL use:

HttpClient httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(Url);

In case of using HTTPS. In Android, your main activity:

protected override void OnCreate(Bundle bundle)
{
    ServicePointManager.ServerCertificateValidationCallback +=(sender, cert, chain, sslPolicyErrors) => true;
}

In iOS, in your AppDelegate:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

    return base.FinishedLaunching(app, options);
}
Anas EL HAJJAJI
  • 1,048
  • 2
  • 10
  • 22
  • It doesn't seem to work with https. Is this supposed to work with https? – Farid Jan 23 '17 at 14:10
  • if the certificate is untrusted, you should use: ServerCertificateValidationCallback, and accept it yourself to establish TLS session – Anas EL HAJJAJI Jan 23 '17 at 14:15
  • You mean something like this: http://stackoverflow.com/questions/22251689/make-https-call-using-httpclient? The WebRequestHandler class isn't in XF. How do I establish TLS? – Farid Jan 23 '17 at 14:19
  • I mean this only: ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; – Anas EL HAJJAJI Jan 23 '17 at 14:23
  • Okay, I'm totally out of my depth here. Can you give a full example please? I'm not a web dev, just doing this as self learning. How / where do I subscribe the ServerCertificateValidationCallback? – Farid Jan 23 '17 at 14:25
  • Okay, I'll complete my example in the answer. – Anas EL HAJJAJI Jan 23 '17 at 14:28