我试图获得一些关于分块endpoint的见解,因此计划逐块打印服务器发送给我的内容。我没有这样做,所以我写了一个测试,看看OkHttp/Retrofit是否像我期望的那样工作。
下面的测试应该会将一些块传递到控制台,但我得到的只是完整的响应。
我有点失去了我错过了什么,甚至让OkHttp3的MockWebServer给我发送块。
我发现了这个改造问题条目,但对我来说答案有点模糊:分块传输编码响应
class ChunkTest {
@Rule
@JvmField
val rule = RxImmediateSchedulerRule() // custom rule to run Rx synchronously
@Test
fun `test Chunked Response`() {
val mockWebServer = MockWebServer()
mockWebServer.enqueue(getMockChunkedResponse())
val retrofit = Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(OkHttpClient.Builder().build())
.build()
val chunkedApi = retrofit.create(ChunkedApi::class.java)
chunkedApi.getChunked()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
System.out.println(it.string())
}, {
System.out.println(it.message)
})
mockWebServer.shutdown()
}
private fun getMockChunkedResponse(): MockResponse {
val mockResponse = MockResponse()
mockResponse.setHeader("Transfer-Encoding", "chunked")
mockResponse.setChunkedBody("THIS IS A CHUNKED RESPONSE!", 5)
return mockResponse
}
}
interface ChunkedApi {
@Streaming
@GET("/")
fun getChunked(): Flowable<ResponseBody>
}
测试控制台输出:
Nov 06, 2018 4:08:15 PM okhttp3.mockwebserver.MockWebServer$2 execute
INFO: MockWebServer[49293] starting to accept connections
Nov 06, 2018 4:08:15 PM okhttp3.mockwebserver.MockWebServer$3 processOneRequest
INFO: MockWebServer[49293] received request: GET / HTTP/1.1 and responded: HTTP/1.1 200 OK
THIS IS A CHUNKED RESPONSE!
Nov 06, 2018 4:08:15 PM okhttp3.mockwebserver.MockWebServer$2 acceptConnections
INFO: MockWebServer[49293] done accepting connections: Socket closed
我希望更像(主体“切割”每5个字节):
Nov 06, 2018 4:08:15 PM okhttp3.mockwebserver.MockWebServer$2 execute
INFO: MockWebServer[49293] starting to accept connections
Nov 06, 2018 4:08:15 PM okhttp3.mockwebserver.MockWebServer$3 processOneRequest
INFO: MockWebServer[49293] received request: GET / HTTP/1.1 and responded: HTTP/1.1 200 OK
THIS
IS A
CHUNKE
D RESPO
NSE!
Nov 06, 2018 4:08:15 PM okhttp3.mockwebserver.MockWebServer$2 acceptConnections
INFO: MockWebServer[49293] done accepting connections: Socket closed
OkHttp Mockserver确实会对数据进行分块,但是看起来LoggingInterceptor会等到整个分块缓冲区已满,然后显示它。
从这个关于HTTP流的很好的总结:
Transtrans-Encode: chunked的使用允许在单个请求或响应中进行流式传输。这意味着数据以分块方式传输,不会影响内容的表示。
考虑到这一点,我们正在处理1个“请求/响应”,这意味着我们必须在获得整个响应之前进行块检索。然后将每个块推送到我们自己的缓冲区中,所有这些都在OkHttp网络拦截器
上。
以下是NetworkInterceptor
的示例:
class ChunksInterceptor: Interceptor {
val Utf8Charset = Charset.forName ("UTF-8")
override fun intercept (chain: Interceptor.Chain): Response {
val originalResponse = chain.proceed (chain.request ())
val responseBody = originalResponse.body ()
val source = responseBody!!.source ()
val buffer = Buffer () // We create our own Buffer
// Returns true if there are no more bytes in this source
while (!source.exhausted ()) {
val readBytes = source.read (buffer, Long.MAX_VALUE) // We read the whole buffer
val data = buffer.readString (Utf8Charset)
println ("Read: $readBytes bytes")
println ("Content: \n $data \n")
}
return originalResponse
}
}
然后我们当然在OkHttp客户端上注册这个网络拦截器。