okhttp3.14.x 上傳不存在的文件後,再上傳文件400

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



    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章