gpt4 book ai didi

ios - AFNetworking:我的类 NSURLResponse 中的 setRedirectResponseBlock 总是返回空值

转载 作者:行者123 更新时间:2023-11-28 22:29:35 25 4
gpt4 key购买 nike

我正在使用 AFNetworking 类与网络服务交互。我在这方面遇到了一些问题。

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:theRequest];// using AFHTTPRequestOperation to request Data
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
NSLog(@"connection :%@",connection);

NSLog(@"request :%@",request);

NSLog(@"response :%@",redirectResponse); question 1:? i get NSURLResponse value is null. output:response :(null)

return request;

}];

在断点的帮助下,我在 AFNetworking 类(类名:AFURLConnectionOperation.m)中检查它是否从服务器获得响应?是的,它得到了回应。

- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block {

self.redirectResponse = block;

NSLog(@"connection :%@",_connection);

NSLog(@"request :%@",_request);

NSLog(@"response :%@",_redirectResponse);//getting NSURLResponse from server.(its showing response) output: response :<__NSGlobalBlock__: 0x46238>

NSLog(@"block :%@",block);

}

我的问题是,当我在我的类中调用 [operation setRedirectResponseBlock.....] 时,NSURLResponse *redirectResponse 始终具有空值,但是当我 checkin 时 - (void)setRedirectResponseBlock..... 在 AFURLConnectionOperation.m 类中NSURLResponse *redirectResponse 有一定的值(value)。你能帮忙知道我哪里错了吗?上周一整天我都在谷歌搜索我找不到与此相关的任何答案。

我正在使用“theRequest.cachePolicy = NSURLRequestUseProtocolCachePolicy;”因为我不想一直从服务器加载只有在 header 中有任何修改时我才想再次调用。为此,我需要使用“setRedirectResponseBlock”来编写以下代码。

if ([response isKindOfClass:[NSHTTPURLResponse self]]) {

NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];

NSString *modified = [headers objectForKey:@"Last-Modified"];


if (modified) { .......

请帮助我解决这个问题,非常感谢示例。

最佳答案

根据相应委托(delegate)方法的文档,connection:willSendRequest:redirectResponse::

The delegate can receive this message as a result of modifying a request before it is sent, for example to transform the request’s URL to its canonical form. To detect this case, examine redirectResponse; if it is nil, the message was not sent due to a redirect.

关于ios - AFNetworking:我的类 NSURLResponse 中的 setRedirectResponseBlock 总是返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17854142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com