AFNetworking:类NSURLResponse中的setRedirectResponseBlock始终返回null值 [英] AFNetworking: setRedirectResponseBlock inside my class NSURLResponse always return null value

查看:114
本文介绍了AFNetworking:类NSURLResponse中的setRedirectResponseBlock始终返回null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using AFNetworking classes to interact with web-service.I am having some problem in that.

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;

    }];

借助Breakpoint,我在AFNetworking类(类名:AFURLConnectionOperation.m)内检查是否从服务器得到响应?是的,它正在得到响应。

With the help of Breakpoint i check inside the AFNetworking classes (class name : AFURLConnectionOperation.m) whether it getting response from server? Yes its getting response.

- (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中的值始终为空,但是当我签入AFURLConnectionOperation.m内部的-(void)setRedirectResponseBlock ......类NSURLResponse * redirectResponse具有某些值。您能帮我知道我错在哪里吗?上个星期我整天都在搜寻,因此找不到与此相关的任何答案。

My Problem is that when i call [operation setRedirectResponseBlock.....] inside my class NSURLResponse *redirectResponse always have null value but when i check in - (void)setRedirectResponseBlock..... inside the AFURLConnectionOperation.m class NSURLResponse *redirectResponse have some value. Can you Please Help to know that where i am wrong. last one week all days i was googling i couldn't find any answer that related to this.

我正在使用 theRequest.cachePolicy = NSURLRequestUseProtocolCachePolicy;因为我不希望仅在标头中进行任何修改时才从服务器一直加载,所以我只想再次调用。为此,我需要使用 setRedirectResponseBlock编写以下代码。

i am using "theRequest.cachePolicy = NSURLRequestUseProtocolCachePolicy;" because i don't want to load all the time from server only if there any modification in header then only i want to call again. for this i need to use "setRedirectResponseBlock" to write below code.

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

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

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


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

请帮我解决这个问题,值得赞赏的例子。

Please Help me to solve this, examples much appreciated.

推荐答案

根据相应委托方法的文档 connection:willSendRequest:redirectResponse:

According to the documentation for the corresponding delegate method, connection:willSendRequest:redirectResponse::


委托可以因此收到此消息在发送请求之前修改请求的方法,例如将请求的URL转换为规范形式。要检测这种情况,请检查redirectResponse;如果为nil,则由于重定向而未发送该消息。

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.

这篇关于AFNetworking:类NSURLResponse中的setRedirectResponseBlock始终返回null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆