B4J Question OkHttp - HTTP/2 support

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,
Http/2 is supported by OkHttp.
However, when I request the following test api: https://http2.pro/api/v1 I get the result, that HTTP/2 is not supported. So my question now, does the latest B4J OkHttp library really support HTTP/2?

Jan
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

it's just a simple Get request:

B4X:
Sub TestHttp2
    Dim H As HttpJob
    H.Initialize("",Me)
    H.Download("https://http2.pro/api/v1")
    
    Wait For (H) JobDone(J As HttpJob)
    
    Log(J.GetString)
End Sub

Jan
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi,

it's just a simple Get request:

B4X:
Sub TestHttp2
    Dim H As HttpJob
    H.Initialize("",Me)
    H.Download("https://http2.pro/api/v1")
  
    Wait For (H) JobDone(J As HttpJob)
  
    Log(J.GetString)
End Sub

Jan
You'll have to use http source class modules ("HttpJob.bas" "HttpUtils2Service.bas") not the jokhttputils library () and in the "HttpUtils2Service.bas" change the hc initialization to
B4X:
hc.InitializeAcceptAll("hc")
(source modules: https://www.b4x.com/android/forum/t...putils2-httputils2-source-code.82632/#content

hc.InitializeAcceptAll skips the certificate verification step

With that, this is the result:

B4X:
   Dim H As HttpJob
   H.Initialize("",Me)
   H.Download("https://http2.pro/api/v1")
  
   Wait For (H) JobDone(J As HttpJob)
   If J.Success Then
       Log(J.GetString)
   Else
       Log(J.ErrorMessage)
   End If

{"http2":0,"protocol":"HTTP\/1.1","push":0,"user_agent":"okhttp\/3.5.0"}

See also: https://www.b4x.com/android/forum/t...liente-to-server-using-ssl.94727/#post-599293
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Thank you, but I don't have any problem with the SSL certificate.
This question is about the protocol used by OkHttp. In your response
{"http2":0,"protocol":"HTTP\/1.1","push":0,"user_agent":"okhttp\/3.5.0"}
you can see that Http 1.1 was used, when you open the Api url in a modern browser you will see that the value for http2 becomes 1.

Jan
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Based on this SO.
best is to use java 9 i guess. But i don´t know if that helps.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I just searched for java 9 and realized that b4j can work with openjava 11
See https://www.b4x.com/b4j.html#installation

I just configured my b4j (V7.0) to use the open java 11. and tried the code above

B4X:
    Dim H As HttpJob
    H.Initialize("",Me)
    H.Download("https://http2.pro/api/v1")
 
    Wait For (H) JobDone(J As HttpJob)
    If J.Success Then
        Log(J.GetString)
    Else
        Log(J.ErrorMessage)
    End If

This is the result:
{"http2":1,"protocol":"HTTP\/2.0","push":1,"user_agent":"okhttp\/3.5.0"}

As a fazit i would suggest to switch to OpenJDK 11
 
Last edited:
Upvote 0
Top