其他
原来OkHttp的拦截器还能这样用
https://juejin.im/post/5afc1706518825426f30f6ec
URL重定向 请求体数据加密 HEAD动态添加 请求日志抓取
@Override
public Response intercept(Chain chain) throws IOException {
return null;
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient build = new OkHttpClient.Builder()
.addInterceptor(new TestInterceptor())
.build();
}
}
private String newHost = "127.0.0.1";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.url();
//http://127.0.0.1/test/upload/img?userName=xiaoming&userPassword=12345
String scheme = url.scheme();// http https
String host = url.host();// 127.0.0.1
String path = url.encodedPath();// /test/upload/img
String query = url.encodedQuery();// userName=xiaoming&userPassword=12345
StringBuffer sb = new StringBuffer();
String newUrl = sb.append(scheme).append(newHost).append(path).append("?").append(query).toString();
Request.Builder builder = request.newBuilder()
.url(newUrl);
return chain.proceed(builder.build());
}
}
private String newHost = "127.0.0.1";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.url();
//http://127.0.0.1/test/upload/img?userName=xiaoming&userPassword=12345
String scheme = url.scheme();// http https
String host = url.host();// 127.0.0.1
String path = url.encodedPath();// /test/upload/img
String query = url.encodedQuery();// userName=xiaoming&userPassword=12345
StringBuffer sb = new StringBuffer();
sb.append(scheme).append(newHost).append(path).append("?");
Set<String> queryList = url.queryParameterNames();
Iterator<String> iterator = queryList.iterator();
for (int i = 0; i < queryList.size(); i++) {
String queryName = iterator.next();
sb.append(queryName).append("=");
String queryKey = url.queryParameter(queryName);
//对query的key进行加密
sb.append(CommonUtils.getMD5(queryKey));
if (iterator.hasNext()) {
sb.append("&");
}
}
String newUrl = sb.toString();
Request.Builder builder = request.newBuilder()
.url(newUrl);
return chain.proceed(builder.build());
}
}
private String newHost = "127.0.0.1";
public static String requestBodyToString(RequestBody requestBody) throws IOException {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
return buffer.readUtf8();
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.url();
//http://127.0.0.1/test/upload/img?userName=xiaoming&userPassword=12345
String scheme = url.scheme();// http https
String host = url.host();// 127.0.0.1
String path = url.encodedPath();// /test/upload/img
String query = url.encodedQuery();// userName=xiaoming&userPassword=12345
StringBuffer sb = new StringBuffer();
sb.append(scheme).append(newHost).append(path).append("?");
Set<String> queryList = url.queryParameterNames();
Iterator<String> iterator = queryList.iterator();
for (int i = 0; i < queryList.size(); i++) {
String queryName = iterator.next();
sb.append(queryName).append("=");
String queryKey = url.queryParameter(queryName);
//对query的key进行加密
sb.append(CommonUtils.getMD5(queryKey));
if (iterator.hasNext()) {
sb.append("&");
}
}
String newUrl = sb.toString();
RequestBody body = request.body();
String bodyToString = requestBodyToString(body);
TestBean testBean = GsonTools.changeGsonToBean(bodyToString, TestBean.class);
String userPassword = testBean.getUserPassword();
//加密body体中的用户密码
testBean.setUserPassword(CommonUtils.getMD5(userPassword));
String testGsonString = GsonTools.createGsonString(testBean);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), testGsonString);
Request.Builder builder = request.newBuilder()
.post(requestBody)
.url(newUrl);
return chain.proceed(builder.build());
}
}
private String newHost = "127.0.0.1";
private String path1 = "/test/upload/img";
private String path2 = "/test/upload/voice";
public static String requestBodyToString(RequestBody requestBody) throws IOException {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
return buffer.readUtf8();
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.url();
//http://127.0.0.1/test/upload/img?userName=xiaoming&userPassword=12345
String scheme = url.scheme();// http https
String host = url.host();// 127.0.0.1
String path = url.encodedPath();// /test/upload/img
String query = url.encodedQuery();// userName=xiaoming&userPassword=12345
StringBuffer sb = new StringBuffer();
sb.append(scheme).append(newHost).append(path).append("?");
Set<String> queryList = url.queryParameterNames();
Iterator<String> iterator = queryList.iterator();
for (int i = 0; i < queryList.size(); i++) {
String queryName = iterator.next();
sb.append(queryName).append("=");
String queryKey = url.queryParameter(queryName);
//对query的key进行加密
sb.append(CommonUtils.getMD5(queryKey));
if (iterator.hasNext()) {
sb.append("&");
}
}
String newUrl = sb.toString();
RequestBody body = request.body();
String bodyToString = requestBodyToString(body);
TestBean testBean = GsonTools.changeGsonToBean(bodyToString, TestBean.class);
String userPassword = testBean.getUserPassword();
//加密body体中的用户密码
testBean.setUserPassword(CommonUtils.getMD5(userPassword));
String testGsonString = GsonTools.createGsonString(testBean);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), testGsonString);
Request.Builder builder = request.newBuilder()
.post(requestBody)
.url(newUrl);
switch (path) {
case path1:
builder.addHeader("token","token");
break;
case path2:
builder.addHeader("token","token");
builder.addHeader("uid","uid");
break;
}
return chain.proceed(builder.build());
}
}
private String newHost = "127.0.0.1";
private String path1 = "/test/upload/img";
private String path2 = "/test/upload/voice";
private String TAG = "TestInterceptor";
public static String requestBodyToString(RequestBody requestBody) throws IOException {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
return buffer.readUtf8();
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
HttpUrl url = request.url();
//http://127.0.0.1/test/upload/img?userName=xiaoming&userPassword=12345
String scheme = url.scheme();// http https
String host = url.host();// 127.0.0.1
String path = url.encodedPath();// /test/upload/img
String query = url.encodedQuery();// userName=xiaoming&userPassword=12345
RequestBody body = request.body();
String bodyToString = requestBodyToString(body);
Log.e(TAG,scheme);
Log.e(TAG,host);
Log.e(TAG,path);
Log.e(TAG,query);
if (response != null) {
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
Log.e(TAG,response.code() + ' '
+ response.message() + ' '
+ response.request().url()+' '
+ bodySize
);
Headers headers = response.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
Log.e(TAG,headers.name(i) + ": " + headers.value(i));
}
}
return chain.proceed(request);
}
}