Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.hjq.demo.http.request;
import com.google.gson.Gson;
import com.hjq.demo.http.response.RsaResp;
import com.hjq.demo.other.DebugLogUtil;
import com.hjq.demo.other.IntentKey;
import com.hjq.demo.other.rsa.RSAUtils;
import com.hjq.toast.ToastUtils;
import com.jh.qiti.util.StringUtil;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
/**
* 车主端
*/
public class OkhttpIntercaptor implements Interceptor {
private String mLanguage;
private String mToken;
OkhttpIntercaptor(String language, String token) {
mToken = token;
mLanguage = language;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request oldRequest = chain.request();
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
//获取未加密数据
RequestBody oldRequestBody = oldRequest.body();
Buffer requestBuffer = new Buffer();
oldRequestBody.writeTo(requestBuffer);
String oldBodyStr = requestBuffer.readUtf8();
requestBuffer.close();
// 进行加密
String newBodyStr = RSAUtils.encrypt("", oldBodyStr);
String encoded = "";
try {
encoded = URLEncoder.encode(newBodyStr, "utf8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
encoded = "";
}
RequestBody newBody = RequestBody.create(mediaType, "rqdata=" + encoded);
// 添加新的参数
HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
.newBuilder()
.scheme(oldRequest.url().scheme())
.host(oldRequest.url().host());
// .addQueryParameter(IntentKey.Cabinet, MmkvUtil.getInt(IntentKey.Cabinet, 0) + "");
// 新的请求
Request.Builder builder = oldRequest.newBuilder()
.method(oldRequest.method(), newBody).
// addHeader("Content-type", "application/json;charset=UTF-8").
url(authorizedUrlBuilder.build());
// mToken = "056b3b0647367ec535ecca4ecc1425305f44a2e0 ";
if (!mToken.equals("")) {
builder.addHeader(IntentKey.TOKEN, mToken);
}
Request newRequest = builder.build();
Response response = chain.proceed(newRequest);
response = decrypt(response);
if (response != null) {
DebugLogUtil.getInstance().Error("-----" + oldRequest.url().toString() + "," + response.body().string());
}
//响应
// Response.Builder builder1 = response.newBuilder().resp
response = chain.proceed(newRequest);
// response.close();
return response;
}
private Response decrypt(Response response) {
ResponseBody oldResponseBody = response.body();
String oldResponseBodyStr = null;
try {
oldResponseBodyStr = oldResponseBody.string();
} catch (IOException e) {
e.printStackTrace();
}
// 需要先将json格式数据取出来
Gson gson = new Gson();
RsaResp decryData = gson.fromJson(oldResponseBodyStr, RsaResp.class);
if (decryData == null || decryData.getFhdata() == null) {
ToastUtils.show("返回值有误");
return null;
}
String decyiedStr = decryData.getFhdata();
String newResponseBodyStr = RSAUtils.decrypt("", decyiedStr);
if (newResponseBodyStr == null) {
return null;
}
String decode = "";
decode = StringUtil.UnicodeToCN(newResponseBodyStr);
oldResponseBody.close();
//构造新的response
ResponseBody newResponseBody = ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), decode);
response = response.newBuilder().body(newResponseBody).build();
//返回
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment