增加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;
}
}