Forum Xamarin Xamarin.Android

logging in to WEB API service fails with 3g but fine with WIFI

AntonvanderVegtAntonvanderVegt AUMember
edited November 2014 in Xamarin.Android

I am using a standard set of code to login to a WEB API server. The code is written below. I have it housed in a Portable library. When I run this code using WIFI, no problem, but when I switch over to the mobile's data (3g), it fails with ex.Message = "Error: ConnectFailure (Connection timed out)".

On the same phone I run a similar piece of java code using 3g, and it works perfectly well.

`        public async Task<bool > canLogin_async()
            HttpClient client = new HttpClient();
            client.Timeout = new TimeSpan(0, 0, 0, 120);
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            List<KeyValuePair<string,string>> urlEncodedList = new List<KeyValuePair<string,string>>();
            urlEncodedList.Add(new KeyValuePair<string,string>("grant_type", "password"));
            urlEncodedList.Add(new KeyValuePair<string, string>("username", accessData.AccessUserName ));
            urlEncodedList.Add(new KeyValuePair<string, string>("password", accessData.AccessPassword ));
            FormUrlEncodedContent content = new FormUrlEncodedContent(urlEncodedList);
            Uri address = new Uri(HOST_URL + TOKEN_ACCESS_URL);
            HttpResponseMessage response;
                response = await client.PostAsync(address , content);
            catch (Exception ex)
                SystemLogManager.AddErrorToLog(mLocalDataConnection, BL.SystemLogLocation.Library, "Failed to get bearer token with error: " + ex.Message , false);
                return false;

            if (response.IsSuccessStatusCode == true)

                string responseString = await response.Content.ReadAsStringAsync();
                JObject jObject = JObject.Parse(responseString);
                string bearerToken = jObject.Value<string>(WebApiAccessHelper.JSON_ACCESS_TOKEN_TOKEN );
                string expireIntervalInSeconds = jObject.Value<string>(WebApiAccessHelper.JSON_EXPIRES_IN_TOKEN );

                return accessData.canSaveNewBearerToken(bearerToken, expireIntervalInSeconds);
                SystemLogManager.AddErrorToLog(mLocalDataConnection, BL.SystemLogLocation.Library, "Failed to get bearer token with error: " + response.StatusCode.ToString() + ":" + response.ReasonPhrase,false );
                return false;


Any ideas most appreciated.


  • kizanlikkizanlik TRMember ✭✭


    I have similar issue on Huawei brand G610 model smartphone.

    The SP has the same issue with WhatsApp which runs using wifi without a problem but 3G.

    It seems device specific issue to me. I haven't tried to find a solution yet because our app runs on some other devices using either wifi or 3G.

    Have you ever tested your app on different brand/model device? If not give a shot.

  • No - but I do run a straight Android Java app with a nearly identical web API client which works perfectly well on the handset (Samsung young). Also - isn't that the point of having targeting an android version - that we get the same results no matter what the handset?
    Thanks for your help - but I need to resolve this issue.

  • kizanlikkizanlik TRMember ✭✭


    I found the source of the problem.

    Usually SP connects to the internet via "internet" apn but ours, somehow, connected to the internet via "wap" protocol.

    I switched the protocol from wap to internet, the problem is solved.

  • This worked :-)
    Thanks heaps Sevket. Do you have any idea why this is the case?? As I mentioned, I use this phone to access a similar API using straight Java, and it does not have a problem with the WAP access?

  • ps: How do I mark your response as the answer - there doesn't seem to be a way of doing this?

  • CheesebaronCheesebaron DKInsider, University mod
    edited November 2014

    You can only do that if you created the post as a question.

    Also when pasting code into the forum, make sure to mark it and press the C button in the toolbar. Don't use ``` around your code unless it is inline like this.

  • kizanlikkizanlik TRMember ✭✭

    Hi Anton,

    I'm happy to hear good news from you. :)

    I have no idea why Java code works but I believe that you make API calls by HttpGet in Java. I think that Apache's HttpClient handles requests in different way (may be we are missing something).

    I'll try to find a solution for this. If I find, I'll let you know.

Sign In or Register to comment.