增加NetworkInterceptor,如下
/**
* 解决在okhttp3的3.14.x版本,当上传不存在的文件时,再重新上传存在的文件,okhttp还复用上次的连接,但是
* 上次的连接中已经write了部分数据,此脏数据导致发生400错误
*
*
*/
public class RemoveDirtyConnIntercepter implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Connection connection = chain.connection();
Response response = null;
try{
response = chain.proceed(request);
}catch (IOException e){
if(connection instanceof RealConnection) {
RealConnection realConnection = (RealConnection) connection;
Method noNewExchanges = RefUtil.getDeclaredMethod(realConnection,"noNewExchanges");
if(noNewExchanges!=null){
try{
noNewExchanges.invoke(realConnection);
}catch (Exception f){
f.printStackTrace();
}
}
}
throw e;
}
return response;
}
}