Android Question HTTP Request connection oddity

Derek Jee

Active Member
Licensed User
Longtime User
Hi There

My app makes many requests to my server, only on one of them it has an issue with the following message. I retry the request immediately and have success most times but definitely after a third time is sends ok.. The message is

The client failed to send a complete request on this REUSED connection before the timeout period elapsed; 0 bytes were read from client.

It appears to happen and the request never gets sent out giving me a null pointer error on the HTTPUtils2 lib.. All of the other requests never have this happen to them. It is odd..

Any ideas anyone? It is a bit vague I guess but an oddity just the same..

Thank you,


Derek.
 

DonManfred

Expert
Licensed User
Longtime User
Without seeing your code we just can guess or ask our hourglas
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
Hello @Derek Jee

As @DonManfred has already mentioned, it's really difficult to state what the issue could be without having more information about the server and client side apps.

My crystal ball suggests that you might try checking that you are 'flushing' the data from the server - assuming that you have written or have access to the server side code.

Good luck!
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Hi gents

Thank you and sorry the code was not there. Just wanted an explanation of the message but I guess the code is needed too. This code works sometimes the first time, sometimes the second with the same code and then the third attempt always works if it gets there.. Ed, I have access to the server side code which is VB ASP pages. The page it is communicating with has all of the response flush code in the right place if that is what you mean.. I have many pages and they all have been set up the same just this one does this odd thing. All headers are populated correctly at the time of sending. Any advice would be grateful..

Derek.

B4X:
    Dim GetActivityListNow As HttpJob
    GetActivityListNow.Initialize("GetActivityListNow", Me, "")
    Dim Value As String = SQL1.ExecQuerySingleResult("SELECT strToken || ' ' || strUserID FROM Parameters Where id = 1")
    Dim ListDate As String = SQL1.ExecQuerySingleResult("SELECT IFNull(strActivityListDate, '01/01/1900 00:00:00') FROM Parameters Where id = 1")
    GetActivityListNow.Download(Main.serverPath & "/Activities/")
    GetActivityListNow.GetRequest.SetHeader("Authorization",Value)
    GetActivityListNow.GetRequest.SetHeader("Version", Main.versionCode)
    GetActivityListNow.GetRequest.SetHeader("If-Modified-Since", ListDate)
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
ps Ed, the request is not getting out of the device so don't think the code on the server is an issue.. Maybe a job done not released?
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
Hello @Derek Jee

It might be helpful to add some simple logging to the code to make sure that there are results coming back from the SQL queries. Also, looking at the example code, the Initialize method of HttpJob takes 2 arguments and not 3 - most likely a cut'n'paste error ;)

Aside from that, have you put in place the JobDone method in your code? If yes, does it get called and if it does is it successful?
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Hi Ed

Thanks, I already had the log in place and all values are returned as I want them on failed ones and successes. We had to add a third argument to the initialize but this error was happening before this was changed.

The job done is there and working ok. When I get the error I retry immediately and it works as needed. I just cannot see why it fails the first time with this reused connection error. It definitely is a time out but not sure what is timing out.. It all looks correct, I have two here looking at the code..

Thank you for your response Ed..
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Where is JobDone sub?

Hi Erel,

Here is the JobDone.. I have seriously stripped the jobdone down to Job.Release and it still errors with the above message. It looks like it is some other request run before this is playing havoc with it but I cannot see what is going on.. But I am pretty sure this JobDone is ok as eliminating it did not solve anything. That message above is not found anywhere on the internet and guess the words REUSED connection is replaced by something else.. I'd love to know what that error message is relating to..

Kind regards,


B4X:
Private Sub JobDone (Job As HttpJob)

    Dim OrigFormat As String=DateTime.DateFormat  'save orig date format
    DateTime.DateFormat="dd-MMM-yyyy HH:mm:ss"
    Dim MyDate As String=DateTime.Date(DateTime.Now)
    DateTime.DateFormat=OrigFormat
    Log("Started ActivityList Job Done " & MyDate)

    Dim x As Int = Job.tag
   
    Log(x)

    If Job.Success Then

        Dim Headers As Map= Job.HTag

        Dim v As String = Headers.Get("Content-MD5")
        Dim IMS As String = Headers.Get("Is-Modified-Since")
        'Dim NewActCount As String = Headers.Get("X-ActivityCount")
        Dim y As String = Job.GetString   
        Log(y)
        Dim md As MessageDigest
        Dim ByteCon As ByteConverter
        Dim passwordhash() As Byte

        passwordhash = md.GetMessageDigest(y.GetBytes("UTF8"),"MD5")
        Dim md5string As String
        md5string = ByteCon.HexFromBytes(passwordhash)
        v = v.SubString2(1,v.Length - 1)
        If md5string <> v Then
            Job.Release
            Main.ActivityListInProgress = False
            StartService(ServicePolling)
            Return
        End If
       
        'Parse JSON

        Dim parser As JSONParser
        parser.Initialize(y)
        SQL1.ExecNonQuery("Delete From tmpActivityList")       
        If y <> "{""ActivityList"": ]}" Then

            Try
                Dim root As Map = parser.NextObject
            Catch
                Job.Release
                Main.ActivityListInProgress = False
                SQL1.ExecNonQuery("Update Parameters Set strActivityListDate = '" & IMS & "', strActivityListNextPoll = '" & MyDate & "'")
                Return
            End Try
            Dim ActivityList As List = root.Get("ActivityList")
       
            SQL1.BeginTransaction

            Try   
                For Each colActivityList As Map In ActivityList
               
                    Dim Col1 As String = colActivityList.Get("PropertyfK")
                    Dim Col2 As String = colActivityList.Get("dteLastAmended")
                    Dim Col3 As String = colActivityList.Get("strInspectionType")
                    Dim Col4 As String = colActivityList.Get("dtePlannedActivity")
                    Dim Col5 As String = colActivityList.Get("strActivityType")
                    Dim Col6 As String = colActivityList.Get("Address")
                    Dim Col7 As String = colActivityList.Get("ActivityPK")
                    Dim Col8 As String = colActivityList.Get("strAccommodationID")
                    Dim Col9 As String = colActivityList.Get("dtePlannedActivity")
                    Col9 = Col9.SubString2(6,10) & Col9.SubString2(3,5) & Col9.SubString2(0,2) & Col9.SubString2(11,13) & Col9.SubString2(14,16)
                    Dim SQL As String = "INSERT INTO tmpActivityList (PropertyFK, dteLastAmended, strInspectionType, dtePlannedActivity, strActivityType, Address, ActivityPK, strAccommodationID, dtePlannedActivityOrder) "   
                        SQL = SQL & " VALUES ('" & RepApp(Col1) & "', '" & RepApp(Col2) & "', '" & RepApp(Col3) & "', '" & RepApp(Col4) & "', '" & RepApp(Col5) & "', '" & RepApp(Col6) & "', '" & RepApp(Col7) & "', '" & RepApp(Col8) & "', '" & RepApp(Col9) & "')"
                    SQL1.ExecNonQuery(SQL)

                Next
               
                SQL1.ExecNonQuery("Update Parameters Set strActivityListDate = '" & IMS & "', strActivityListNextPoll = '" & MyDate & "'")

                SQL1.TransactionSuccessful
            Catch
                Log(LastException.IsInitialized)
            End Try   
            SQL1.EndTransaction
           
        Else
           
            SQL1.ExecNonQuery("Update Parameters Set strActivityListDate = NULL Where id = 1")

        End If
       
        SQL1.ExecNonQuery("Update Parameters Set strLastSuccessfulActivityList = '" & MyDate & "'")
           
        Log("Got Activity List")
       
        Main.ActivityListInProgress = False
       
        'Update Veco with versions of activities here           
   
        If Main.ConfirmationInProgress = False Then
            Main.ConfirmationInProgress = True
            SendConfirmation.Initialize
            CallSub(SendConfirmation, "SendConfirmation")
        End If
       
        CompareLists
       
    Else
       
        Dim ResponseList As List
        ResponseList.Initialize
        Dim ResponseItem As JobResponse
        ResponseItem.ResponseCode = Job.Tag
        ResponseItem.JobType = "GetActivityList"
        ResponseItem.RecordID = ""
        ResponseList.Add(ResponseItem)
        ResponseHandler.Initialize
        CallSub2(ResponseHandler, "Handler", ResponseList)
       
    End If
   
    Job.Release
    StartService(ServicePolling)


End Sub
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Hi Erel

I have Fiddler running and the request does not leave the device. I make the request the first time and it times out with that message above and nothing shows on Fiddler. The retry successfully sends the request and shows a trace on Fiddler. On the HTTPUtils2 lib in the sub
hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int) I trap the error and see the following populating the Reason variable, java.net.SocketTimeoutException..
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
To add some more.. If I make a different request the first one sent I get the same error.. I don't think there is anything wrong with the request I am currently sending but something wrong with the last one not finishing or something like that..
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
I am on a real device..

This timeout is happening each time I start processing requests. The first one almost always fails so don't think it is just a random exception..
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
If you start a service it will be restarted if it is already running i think.
Thanks Manfred.. If that service started a HTTP request and then the same service was restarted and ran another HTTP request, would the code instantiated on the first service run in its entirety even though 'its' service was destroyed and another restarted.. I am wondering if a JobDone from a previous request never got started leaving a job open causing the next attempt to fail and then try again successfully..
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
Hello @Derek Jee

I've just been catching up on the progress so far...what is the server path you're using? Is it a domain name or an IP address? The reason I ask is that if it's a domain name then it could be DNS related and it might be worth trying the IP address directly. If you ping the domain name several times in succession do you consistently get the same IP address pinging back? If not then you may be facing an issue with a DNS server fresh issue. If that is the case you may need to update the DNS server IPs to another DNS provider. Another possibility is the use of a web farm where a load balancer sends requests to different servers that can lead to timeouts if the load balancer is not configured correctly (I've had this issue before with IIS servers).

I'm throwing these ideas/thoughts out there as it looks as though just about everything else has been tried.
 
Upvote 0
Top