okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

java.net.ProtocolException: expected 0 bytes but received 8192

Open vellrya opened this issue 3 years ago • 2 comments

Hello. Long story short:

    val url = "https://www.ozon.ru/"
    val requestBuilder = Request.Builder().url(url)
    requestBuilder.header("User-Agent", "Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.97 Mobile Safari/537.36")
    val response = OkHttpClient.Builder().build().newCall(requestBuilder.get().build()).execute()
    println(response.protocol)
    println(response.header("Content-Length"))
    println(response.headers)
    println(response.body!!.contentLength())
    println(response.body!!.contentType())
    val body = response.body
    val string = body!!.string()
    body.close()
    println(string)

h2 null 0 null Exception in thread "main" java.net.ProtocolException: expected 0 bytes but received 8192 at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:290) at okio.RealBufferedSource.select(RealBufferedSource.kt:229) at okhttp3.internal.Util.readBomAsCharset(Util.kt:265) at okhttp3.ResponseBody.string(ResponseBody.kt:187) at a.b.c.main(Hello.kt:377)

This site works when force Protocol.HTTP_1_1 in OkHttpClient builder, also works perfectly in browser with http2 (either from real device or from PC browser devtools with mobile UA). image

To reproduce this error, you can use any mobile user-agent (I tried 3 different). Tried on OkHttp 4.10.0 / 5.0.0-alpha.10

vellrya avatar Aug 30 '22 13:08 vellrya

Thanks, should be relatively easy to repro with a test server.

yschimke avatar Aug 30 '22 14:08 yschimke

So it's sending an early hints (103).

Issue: https://github.com/square/okhttp/issues/3671 and a PR https://github.com/square/okhttp/pull/7196

yschimke avatar Sep 04 '22 14:09 yschimke

Closing as a duplicate of https://github.com/square/okhttp/issues/3671

yschimke avatar Sep 24 '22 09:09 yschimke