Error getting response stream (ReadDone2): ReceiveFailure

JasonBSteeleJasonBSteele USUniversity ✭✭

Hi,

My Xamarin.Android app uses HTTPClient from Nuget in a PCL.
It makes 3 web calls, the first registers some entered details, the second logs on returning a cookie, the third gets some data using the cookie.

This was working fine in Xamarin.Android version 4.10 but since upgrading to 4.12 I am getting the error "Error getting response stream (readdone2) ReceiveFailure" in the third call.

However if I introduce a delay of 5 seconds before making the third call it works fine (but 2 seconds is too short).

I use using() when instantiating HttpClient and Dispose() of the HttpResponseMessage returned from GetAsync().

Can anyone suggest what the problem is? Is this a bug introduced by Mono?

Posts

  • DualDubDualDub GBMember ✭✭

    I am getting this too. Has anybody any idea of what is going on?
    I tried using webclient instead but that did not help

  • CheesebaronCheesebaron DKInsider, University mod

    Have you tried ModernHttpClient from the component store? It uses alternative handlers but still uses HttpClient.

  • JasonBSteeleJasonBSteele USUniversity ✭✭

    Thanks Cheesebaron - I'll give it a go!

  • JasonBSteeleJasonBSteele USUniversity ✭✭
    edited April 2014

    @Cheesebaron‌ Many thanks for the tip this resolved the issue!

    I used your MvvmCross plugin for this too. I did have some initial problems getting it to work though because it wasn't obvious to me that my main Droid project had to reference OkHttp.dll as well (even though it doesn't directly use it).

    I think this is because OkHttp.dll is a binding project containing an android .jar and the jar is only visible to the app if the dll is directly referenced.

    Might be worth a note in your GitHub readme?

    1. Grab the latest ModernHttpClient binaries or build from source and extract into the vendor
      folder.
    2. Reference okHttp.dll from your main android project
    3. Build
    4. ???
    5. Profit

    :) Cheers!

  • CheesebaronCheesebaron DKInsider, University mod

    Well it spits out everything you need in the bin folder in the root :)

  • AndreasSelleAndreasSelle DEBeta ✭✭

    Hi,

    we are gettig the same "Error getting response stream (readdone2) ReceiveFailure" on some HTTPS requests since upgrading to 4.12. Same code works on 4.10 and on Windows .NET. Changing to ModernHttpClient is no option for us.

  • CheesebaronCheesebaron DKInsider, University mod

    Just curious, why is it not an option?

  • AndreasSelleAndreasSelle DEBeta ✭✭

    Our code base must be 100% portable accross Android, iOS, MacOS X, Windows and Windows Phone. So only .NET API equally available on all platforms is acceptable.

  • blazeblaze USMember ✭✭

    I had the same issue...
    In my case content was represented as ByteArrayContent:
    req.Content = new ByteArrayContent(pEvents.SerializeProtobuf());

    I have changed it to StringContent:
    req.Content = new StringContent(System.Text.Encoding.UTF8.GetString(pEvents.SerializeProtobuf()));

    and changed server side...after that it worked...

  • CheesebaronCheesebaron DKInsider, University mod

    @AndreasSelle‌ which HttpClient which ModerHttpClient uses is. The only difference is the handlers it provides. Sure they are platform specific, you code in your Android and iOS projects are not 100% portable, so your argument fails :)

  • blazeblaze USMember ✭✭

    Further investigation has shown that StringContent has nothing to do with the HttpClient mistake... But I've solved the problem, it differs a bit from the topic.
    I had a timer which calls every 10 seconds HttpClient.SendAsync... and when timer.Elapsed I stop the timer, when send is finished, I start the timer again. In such circumstances I've encountered getResponse Stream error and sometimes the whole app even hanged.

    Then I found out that stopping the timer tmr.Stop and with tmr.Enabled=false... somehow doesn't work and my SendToServer procedure can be called multiple times... I've put mechanical return from the Timer_Elapsed, till SendToServer procedure is finished, after that - no claims, all works...

    So, many things to work out....

  • AndreasSelleAndreasSelle DEBeta ✭✭

    Hi,

    I was hoping that with Xamarin.android 4.12.4 the problem will be fixed, but it wasn't. Thus I now created the smallest possible test code to demonstrate the bug. The method logs into PayPal with the given credentials. It must be called with valid PayPal credentials in order to provoke the failure.

    void PayPalTest(string sUserEMail, string sPassword)
            {
                CookieContainer vCookieContainer = new CookieContainer();
    
                // GET login page, filling in various cookies.
    
                HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/de/cgi-bin/webscr?cmd=_login-run");
                aRequest.CookieContainer = vCookieContainer;
                HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();
                aResponse.Close();
    
                // POST login. User e-mail and password are assumed to not need any HTML encoding.
    
                aRequest = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/de/cgi-bin/webscr?cmd=_login-submit");
                aRequest.CookieContainer = vCookieContainer;
                aRequest.Method = "POST";
    
                string sPostData = "login_email=" + sUserEMail + "&login_password=" + sPassword;
                byte[] vbData = Encoding.UTF8.GetBytes(sPostData);
                using (Stream aStream = aRequest.GetRequestStream())
                {
                    aStream.Write(vbData, 0, vbData.Length);
                    aStream.Close();
                }
    
                try
                {
                    // This is where the
                    // "Error getting response stream (ReadDone2): ReceiveFailure"
                    // exception occurs
                    aResponse = (HttpWebResponse)aRequest.GetResponse();
                }
                catch (Exception x)
                {
                    Toast.MakeText(this, x.Message, ToastLength.Long).Show();
                }
    
                aResponse.Close();
            }
    

    Just paste this method in a default project. Works in Windows.NET, works in Xamarin.Mac and also Xamarin.iOS, but gives the said exception on Xamarin.Android starting with version 4.12.

  • JasonBSteeleJasonBSteele USUniversity ✭✭

    @AndreasSelle‌ Now that you have gone to he trouble of creating a small repo its probably worth logging in https://bugzilla.xamarin.com/ if you haven't done so already.

  • AndreasSelleAndreasSelle DEBeta ✭✭

    Hello,

    I just filed a bug report: https://bugzilla.xamarin.com/show_bug.cgi?id=19673

  • Have there been any updates to this issue? I'm having the issue with this code:

    HttpClient _client = new HttpClient();
    HttpResponseMessage response = await _client.PostAsync(_authUrl, new StringContent(String.Format("{{ \"userName\":\"{0}\", \"password\":\"{1}\" }}", userName, password), Encoding.UTF8, "application/json"));

    _authUrl is the URL i'm trying to post to (using HTTPS), and _client is a System.Net.Http.HttpClient.

  • I am also getting this issue: Error getting response stream (ReadDone2): ReceiveFailure

    It occurs if I try to do a GET every 5 seconds on a timer. If I change the timer to 60 seconds I dont get the error.

  • OzzieOzzie USMember, University

    I had the same issue and ModernHttpClient worked for me. Here's the gist of it:

        using System.Net.Http;
        using ModernHttpClient;
        ...
        var httpClient = new HttpClient(new NativeMessageHandler());
        var downloadedFile = httpClient.GetStreamAsync (url).Result;
    
  • StefanosKaratzogiannisStefanosKaratzogiannis GRUniversity ✭✭

    I was so disappointed to find out about this bug after launching a whole new update of my app on playstore. I even got bad reviews because of it.

    Neither our testers nor me had encountered it, so it slipped. I also had a hard time reproducing it but finally i found a wifi that throws the exception every time. I managed to patch the workaround with modernhttpclient.

    It hasn't been fixed yet on mono, at least on the update i got a couple of days ago, on the stable channel. And the bugzilla report is still at NEW status... Won't you guys fix this?

  • BalakrishnanSBalakrishnanS USMember ✭✭

    I am getting this while uploading the Image after taking the picture in Android alone, It is working fine in IOS.
    May I know the reason and solution for this.

  • DavidMannDavidMann USMember

    I'm getting the same exception when trying to get a response, what is the solution? I have tried using ModernHttpClient and same thing happens.

  • JasonBSteeleJasonBSteele USUniversity ✭✭

    @DavidMann said:
    I'm getting the same exception when trying to get a response, what is the solution? I have tried using ModernHttpClient and same thing happens.

    Have you tried the latest stable release of Xamarin.Android (6.1) It includes support for the Native HttpClientHandler: https://developer.xamarin.com/releases/android/xamarin.android_6/xamarin.android_6.1/#AndroidHttpClientHandler

    Or perhaps you have recently updated to this version and it's the cause of your problem?

  • Umar3xUmar3x FRMember ✭✭✭

    Hello all,

    Still having this issue today. Using System.Net.Http; I don't want to use modernHttpClient as it is not maintained. Does someone meet this prob ? It seems to happen only when the connection is not good (2G/3G or bad wifi signal)

    I try catch the error, but it's a big deal for us as our app is meant to be used outside town so where connections are not that good.

  • UnreachableCodeUnreachableCode USMember ✭✭✭

    I am in the same position as @Umar3x

Sign In or Register to comment.