c# - HttpClient PostAsync on xamarin does nothing -


i have 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 use in desktop app, same piece fails on xamarin.android. can access web site emulators browser, it's not case of not having connection between these two. more interesting part - getasync works absolutely fine. postasync fails taskcancelledexception because of timeout. postasync calls not hit server @ all.
activity 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 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 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 activity behaves same - waits quite long time , fails taskcancelledexception because of timeout.

for struggling same issue - related ssl (self signed cert in case). if trying connect server via https - try using plain http first, app can work http fine while https hangs death.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -