美文网首页
ios请求后获取相关响应头参数

ios请求后获取相关响应头参数

作者: iOS攻城狮_Runloop | 来源:发表于2023-04-11 09:24 被阅读0次

    /*GET请求/

    ···

    • (void)GET:(NSString *)url parameters:(NSDictionary *)params success:(CodeSuccessBlock)success failure:(FailureBlock)failure{

        NSMutableDictionary *totalParams = [NSMutableDictionary dictionary];
        [totalParams addEntriesFromDictionary:[self getCommonParameters]];
        [totalParams addEntriesFromDictionary:params];
        NSLog(@"URL:%@ params:%@",url,totalParams);
      

      //去除掉首尾的空白字符和换行字符
      NSString * removeUrlStr= [url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
      //url字符串UTF-8编码
      NSString *endUrlStr = [removeUrlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

      [self.manager GET:endUrlStr parameters:totalParams headers:self.headParams progress:^(NSProgress * _Nonnull downloadProgress) {

      } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
      dispatch_async(dispatch_get_main_queue(), ^{
      //统一处理Code状态
      NSInteger code = [self judgeCodeHandleWithJsonDic:responseObject];
      NSString * message = responseObject[@"message"];
      if (success) {
      success(code,responseObject,message);

                 //设置响应头数据
                  NSHTTPURLResponse * response = (NSHTTPURLResponse *)task.response;
                  NSDictionary* headFilesDic = response.allHeaderFields;
                  NSLog(@"GET+++响应头:%@ ",headFilesDic);
                 
                 //响应头中获取token参数,含有refresh-token的话,和本地token对比更新token
                 [self refreshTokenWithHeadfilesDic:headFilesDic];
             }
        });
      

      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
      [MBProgressHUD wj_showPlainText:error.description view:nil];
      if (failure) {
      failure(error);
      }
      }];
      }

    #pragma mark - 响应头中获取token对比更新
    -(void)refreshTokenWithHeadfilesDic:(NSDictionary *)headFilesDic{
    NSString * refreshToken = headFilesDic[@"refresh-token"];
    if (refreshToken.length > 0) {
    //对比更新
    NSString * localToken = GetToken;
    if (![localToken isEqualToString:refreshToken]) {
    //更新token
    SaveToken(refreshToken);
    }
    }
    }

    ···

    相关文章

      网友评论

          本文标题:ios请求后获取相关响应头参数

          本文链接:https://www.haomeiwen.com/subject/ofqwddtx.html