B4J Question [SOLVED] Question about OkHttpUtils2 Response.ContentLength

oleg_

Member
Licensed User
Here
https://www.b4x.com/android/forum/threads/getting-http-headers-only.94536/post-598223
ContentLength turns out to be zero.
And I also tried to request the ContentLength of different JPG files on different sites in the same way. ContentLength always turns out to be zero for some reason. Is this how it should be?
Maybe the problem is that sites prohibit downloading headers separately from the content?

B4X:
Private Sub Button1_Click
    Test
End Sub

Sub Test
    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)
    Else
        Log(j.ErrorMessage)
    End If
    j.Release
End Sub


B4J
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
ContentLength = 0

B4A
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
** Activity (main) Pause event (activity is not paused). **
** Activity (main) Resume **
*** Receiver (httputils2service) Receive (first time) ***
ContentLength = 0
 

Attachments

  • Test.zip
    6.6 KB · Views: 32
Last edited:
Solution
The result is this:
Either we have a bug here or a misunderstanding of the Response.ContentLength, since if I do
B4X:
Log(j.Response.GetHeaders.GetDefault("content-length", "NADA"))
I've got a content length
Doing a
B4X:
Log(j.Response.GetHeaders)
shows that the site even supports ranges. I tried this on my site, and I've had the same results.

drgottjr

Expert
Licensed User
Longtime User
content-length is 0 because there is no content. headers are not content.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
content-length is 0 because there is no content.
Actually, if a site supports ranges, content length should be the size of the file to be downloaded. A content length of 0, in this case, means no range support (although if you want to be 100% certain, you check for a accept-ranges header, and if available, the content of that header)
 
Upvote 0

oleg_

Member
Licensed User
content-length is 0 because there is no content. headers are not content.

drgottjr, thank you for the answer.

But based on your answer, I don’t understand what is the point of that fragment of Erel’s code if ContentLength will always be equal to zero?

But in any case, how to find out the size of a JPG file without downloading it, but by downloading only the header?

OliverA, thank you for your opinion.
But your option from that thread, unfortunately, doesn’t work either:
I've already tried it with different pictures on different sites.

B4X:
Sub Test
    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("j.GetString = " & j.GetString)
    Else
        Log(j.ErrorMessage)
    End If
    j.Release
End Sub

The result is this:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
j.GetString =
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The result is this:
Either we have a bug here or a misunderstanding of the Response.ContentLength, since if I do
B4X:
Log(j.Response.GetHeaders.GetDefault("content-length", "NADA"))
I've got a content length
Doing a
B4X:
Log(j.Response.GetHeaders)
shows that the site even supports ranges. I tried this on my site, and I've had the same results.
 
Upvote 0
Solution

oleg_

Member
Licensed User
OliverA, thank you again!

This way it works great!
B4X:
Log(j.Response.GetHeaders.GetDefault("content-length", "NADA"))
 
Upvote 0
Top