我的REST通信已加密,我的请求实际上如下:
@POST("some/address")
fun someMethod(@Body request: EncryptedRequest): Observable<EncryptedResponse>
我想要实现
@POST("some/address")
fun checkUser(@Body request: RealRequest): Observable<RealResponse>
并在飞行中加密/解密请求和响应。
现在我可以使用拦截器来获取请求:
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val oldContent = getOldRequestContent(request)
val mediaType = MediaType.parse("application/json; charset=utf-8")
val encryptedContent = encryptContent(oldContent)
val body = RequestBody.create(mediaType, encryptedContent)
val newRequest = request.newBuilder()
.header("Content-Type", body.contentType()!!.toString())
.header("Content-Length", body.contentLength().toString())
.method(request.method(), body)
.build()
return chain.proceed(newRequest)
}
它工作正常,但是当Retrofit回复时,如何将 EncryptedResponse 强制转换为 RealResponse ?正如我所见,Interceptor没有提供这样的解决方案。