B4J Question Java.net protocol exception (status code 103)

Kevin

Well-Known Member
Licensed User
Longtime User
I am having a recent problem accessing a 3rd party webpage that still works fine in a browser but recently started causing an error in my B4J app when trying to retrieve it.

It appears that it may be related to http status code 103 ("early hints")? I have no control over the server. Is there any way to handle something like this through my B4J code?

Basic log:
Success = false
Status Code = 103

Error log:

java.net.ProtocolException: expected 0 bytes but received 2881
at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:290)
at okio.RealBufferedSource.select(RealBufferedSource.kt:229)
at okhttp3.internal.Util.readBomAsCharset(Util.kt:258)
at okhttp3.ResponseBody.string(ResponseBody.kt:187)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:243)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:577)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1589)
 

Kevin

Well-Known Member
Licensed User
Longtime User
It may be related to CDN and early hints (103).
There is a public page for testing this (https://early-hints.fastlylabs.com/) and it produces the same result I am seeing.

That link should work in a browser, but it causes an error in B4J.

B4X:
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://early-hints.fastlylabs.com/")
Wait For (j) JobDone(j As HttpJob)
Log("Success = " & j.Success)
Log("Response = " & j.Response.StatusCode)
If j.Success Then Log("String = " & j.GetString)
j.Release
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I tried doing that but it didn't seem to help. However, I wasn't really sure if I had it correct or not. I'd look but I'm not at my computer right now.

What B4J code should I use to force the older protocol? I'll try it in the morning.

Is there any way to modify okhttp's behavior by modifying a library or inserting Java code into the B4J code? Hopefully the protocol trick will work, but I'm not sure what else to do if it doesn't.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Call this sub when the app starts:
B4X:
'Ctrl+B and add HU2_PUBLIC as a build symbol
Private Sub SetOkHttpProtocolToHttp1_1
    Dim jo As JavaObject = HttpUtils2Service.hc
    Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
    Dim protocol As JavaObject
    protocol.InitializeStatic("okhttp3.Protocol")
    Dim protocols As List = B4XCollections.CreateList(Array(protocol.RunMethod("valueOf", Array("HTTP_1_1"))))
    builder.RunMethod("protocols", Array(protocols))
    jo.SetField("client", builder.RunMethod("build", Null))
End Sub

Is there any way to modify okhttp's behavior by modifying a library or inserting Java code into the B4J code?
Modifying OkHttp to support this feature is not practical.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I was definitely not setting the protocol correctly. The code you provided to force 1.1 is working better on the page I'm trying to access. It still fails sometimes with code 103, but if I send the request a second time then it usually works.

That said, it isn't returning the full document like it used to before this change, which I suppose is likely a side effect that can't easily be avoided as things are with Okhttp.

It sounds like I may be out of luck on this, but I will do some digging around in the results that I do get back and see if I can figure out any way to find a usable link within them to the info I'm trying to get.

Thanks for your help
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…