Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

TaskCanceledException thrown on await client.PostAsync()

$
0
0

Very strange problem (all code is in PCL),.

When I call await client.PostAsync() against a webservice (API's), I get the TaskCanceledException.

Few things to note:

  1. This is running fine on Android devices. It only happens when I run the app on iOS, but...
  2. ... to make it even stranger, I **can **make it run on iOS if I use different (physical) server that hosts the API's that I query with await client.PostAsync()

So,

https://somedomainname.com - works fine
https://someotherserver.com - does not work

To make it even more strange, both servers run exactly the same webservice, there is 0 difference, the only difference is the servers are on different sites with different client. So the fault is not with the queried webservice throwing timing out or cancelling the request.

Both servers have valid (trusted) SSL certificate so it's not an SSL related issue.

I am stuck. I have already tried ModernHttpClient and increaing the client.TimeSpan.

I also checked the ex.CancellationToken.IsCancellationRequested, which is false, so it's pretty safe to assume it was some time of "timeout". However, when I run the Post request from PostMan manually against that API, it's clear the webservice works fine (there is no timeout).

public class TokenService
 {
        private HttpClient client;
        private App app;

        public TokenService()
        {
            //client = new HttpClient();

            client = new HttpClient(new NativeMessageHandler());
            client.Timeout = TimeSpan.FromMinutes(30);

            client.MaxResponseContentBufferSize = 256000;
            app = (App)Application.Current;
        }

        public class TokenResponse
        {
            public String access_token;
            public Int32 expires_in;
        }

        public async Task<String> GetNewToken()
        {
            // check if valid token already stored and within expiry DateTime

            if(Token.BearerToken != null)
            {
                if (Token.expiry > DateTime.Now)
                    return Token.BearerToken;
            }

            var tokenUlr = app.WebServicesHostName + "/token";

            var tokenResponse = new TokenResponse();

            try
            {
                var content = new FormUrlEncodedContent(new[]
                {
                    // this needs to be stored somewhere safe:
                    new KeyValuePair<string, string>("username", "validusernamehere"),
                    new KeyValuePair<string, string>("password", "validpasshere")
                });

                var response = await client.PostAsync(tokenUlr, content);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var token = await response.Content.ReadAsStringAsync();

                    tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(token);

                    Token.BearerToken = tokenResponse.access_token;
                    Token.expiry = DateTime.Now.AddSeconds(tokenResponse.expires_in - 30);

                    return tokenResponse.access_token;
                }
                else
                {
                    return null;

                }
            }
            catch(TaskCanceledException ex)
            {
                String message = ex.Message;
                String source = ex.Source;
                CancellationToken token = ex.CancellationToken;



                return null;
            }
            catch(Exception ex)
            {
                String test = ex.Message;
                return null;
            }
        }
}

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>