Reputation: 1577
I have a piece of code
var formContent =
new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", _userName),
new KeyValuePair<string, string>("password", _password)
});
var response = await InternalClient.PostAsync("/Token", formContent).ConfigureAwait(false);
It works fine when i use it in my desktop app, but this very same piece fails on Xamarin.Android. I can access my web site from emulators browser, so it's not the case of not having the connection between these two. Even more interesting part - GetAsync works absolutely fine. PostAsync always fails with TaskCancelledException because of timeout. All the PostAsync calls do not hit the server at all.
My activity where this is executed:
var isAuthSuccess = _mainClient.Authenticate();
isAuthSuccess.ContinueWith(task =>
{
RunOnUiThread(() =>
{
if (isAuthSuccess.Result)
{
ReleaseEventHandlers();
var nav = ServiceLocator.Current.GetInstance<INavigationService>();
nav.NavigateTo("MainChatWindow", _mainClient);
}
button.Enabled = true;
});
});
And the Authenticate method:
public async Task<bool> Authenticate()
{
var getTokenOperation = new AsyncNetworkOperation<string>(GetTokenOperation);
var token = await getTokenOperation.Execute().ConfigureAwait(false);
if (getTokenOperation.IsCriticalFailure)
{
SetCriticalFailure(getTokenOperation.FailureReason);
}
if (getTokenOperation.IsFailure == false)
{
InternalClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
return true;
}
else
{
AuthenticationFailEncounteredEvent("Authentication fail encountered: " + getTokenOperation.FailureReason);
return false;
}
}
Get token operation:
private async Task<string> GetTokenOperation()
{
var formContent =
new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", _userName),
new KeyValuePair<string, string>("password", _password)
});
var response = await InternalClient.PostAsync("/Token", formContent).ConfigureAwait(false);
response.EnsureSuccessStatusCodeVerbose();
var responseJson = await response.Content.ReadAsStringAsync();
var jObject = JObject.Parse(responseJson);
var token = jObject.GetValue("access_token").ToString();
return token;
}
And the wrapper - AsyncNetworkOperation
public class AsyncNetworkOperation<T>
{
public bool IsFailure { get; set; }
public bool IsCriticalFailure { get; set; }
public string FailureReason { get; set; }
public bool IsRepeatable { get; set; }
public int RepeatsCount { get; set; }
public Func<Task<T>> Method { get; set; }
public AsyncNetworkOperation(Func<Task<T>> method, int repeatsCount)
{
Method = method;
IsRepeatable = true;
RepeatsCount = repeatsCount;
}
public AsyncNetworkOperation(Func<Task<T>> method)
{
Method = method;
}
public async Task<T> Execute()
{
try
{
return await Method().ConfigureAwait(false);
}
...exception handling logics
}
}
Calling PostAsync right in the activity behaves the same - waits for quite a long time and then fails with TaskCancelledException because of timeout.
Upvotes: 2
Views: 2700
Reputation: 1577
For all the struggling with the same issue - this is related to SSL (self signed cert in my case). If you are trying to connect to your server via HTTPS - try using plain HTTP first, my app can work with HTTP fine while HTTPS hangs to death.
Upvotes: 1
Reputation: 11105
I have had other problems with PostAsync()
also and it is also not as customizable as SendAsync()
in terms of editing the headers and things like that. I would recommend SendAsync()
:
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/Token") { Content = formContent };
HttpResponseMessage message = await InternalClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);
Upvotes: 0