1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
public Response (Chain chain) throws IOException { Request request = chain.request(); String url = request.url().toString(); String method = request.method(); Log.i(TAG, "url为:" + url + "nmethod为:" + method); request = encrypt(request); Response response = chain.proceed(request); response = decrypt(response); return response; }
private Request encrypt(Request request) throws IOException { RequestBody requestBody = request.body(); if (requestBody != null) { okio.Buffer buffer = new okio.Buffer(); requestBody.writeTo(buffer); Charset charset = Charset.forName("UTF-8"); MediaType contentType = requestBody.contentType(); if (contentType != null) { charset = contentType.charset(charset); } String string = buffer.readString(charset); Log.i(TAG, "请求的原数据为:" + string); String encryptStr = encrypt(string); RequestBody body = MultipartBody.create(contentType, encryptStr); Request.Builder builder = request .newBuilder() .post(body) .build(); return request; }
private Response decrypt(Response response) throws IOException { if (response.isSuccessful()) { ResponseBody body = response.body(); BufferedSource source = body.source(); source.request(Long.MAX_VALUE); Buffer buffer = source.buffer(); Charset charset = Charset.defaultCharset(); MediaType contentType = body.contentType(); if (contentType != null) { charset = contentType.charset(charset); } String decryptStr = buffer.clone().readString(charset); String string = decrypt(decryptStr); Log.i(TAG, "响应的数据解密后为:" + string ); ResponseBody responseBody = ResponseBody.create(contentType, bodyString); response = response.newBuilder().body(responseBody).build(); } return response; }
|
近期评论