Bug? Issues with B4A/B4J HttpJob.Response.ContentLength

OliverA

Expert
Licensed User
Longtime User
In B4J and B4A, HttpJob.Response.ContentLength seems to always return 0 when using HttpJob.Head. In iOS, the same method returns the requested information.
(This issue was discovered by @oleg_ : https://www.b4x.com/android/forum/t...t-okhttputils2-response-contentlength.160216/)

This is the code snippet:
B4X:
    Dim filename As String = "https://b4x-4c17.kxcdn.com/android/forum/data/avatars/m/86/86664.jpg?1520274433"
    Dim j As HttpJob
    j.Initialize("", Me)
    j.head(filename)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("ContentLength = " & j.Response.ContentLength)
        Log(j.Response.GetHeaders)
        Log(j.Response.GetHeaders.GetDefault("content-length", "NADA"))
    Else
        Log(j.ErrorMessage)
    End If
    j.Release
(Test project is attached)

These are the various platforms/results:

B4J 10.00
JDK 14.0.1
OkHttpUtils2 3.02
Output:

B4A 12.80
JDK 14.0.1
OkHttpUtils2 3.04
Output:

B4i 8.51
iHttpUtils2 3.04
Output:

For some reason, on B4A/B4J, the content length values are enclosed in [ and ] (all header values seem to be enclosed in such a way). IDK if that interferes with the proper conversion to the value, since when pulling the header value directly (via Response.GetHeaders.GetDefault("content-length", "NADA")) the brackets are still there.
 

Attachments

  • HeaderTestCLv2.zip
    21.7 KB · Views: 208

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the behavior of the underlying OkHttp SDK.

I've used this code in the very useful RangeDownloader module, to get headers values:
B4X:
Private Sub GetCaseInsensitiveHeaderValue (job As HttpJob, Key As String, DefaultValue As String) As String
    Dim headers As Map = job.Response.GetHeaders
    For Each k As String In headers.Keys
        If K.EqualsIgnoreCase(Key) Then
            Return headers.Get(k).As(String).Replace("[", "").Replace("]", "").Trim
        End If
    Next
    Return DefaultValue
End Sub
 

OliverA

Expert
Licensed User
Longtime User

oleg_

Member
Licensed User
I prefer not to change the existing behavior as theoretically it is only in the rare HEAD request where there is a difference, and the workaround is simple.
Perhaps this post
should be edited? In this form, it misleads beginners. Response.ContentLength there will always be equal to zero, and this leads to the erroneous idea that all sites prohibit reading the HEAD separately from the content.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…