gpt4 book ai didi

objective-c - NSURLConnection 连接 : didReceiveData: is not called after redirect

转载 作者:行者123 更新时间:2023-11-29 13:21:47 24 4
gpt4 key购买 nike

我正在启动一个请求,该请求返回带有重定向的 HTML。为什么 didReceiveData 函数没有被调用第二次?我正在尝试下载一个 JSON 文件。 (使用 iOS6)

    - (void) testdownload{
NSURL *url = [NSURL URLWithString:@"https://***];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d{

NSString *tmpdata = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
NSLog(@"data: %@", tmpdata);
}

当我使用 UIWebView 时,将处理重定向并显示 JSON 文件。玩这个功能没用:

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse{
return request;
}

最佳答案

尝试改用这段代码:

- (NSURLRequest *)connection: (NSURLConnection *)inConnection
willSendRequest: (NSURLRequest *)inRequest
redirectResponse: (NSURLResponse *)inRedirectResponse;
{
if (inRedirectResponse) {
NSMutableURLRequest *r = [[request mutableCopy] autorelease]; // original request
[r setURL: [inRequest URL]];
return r;
} else {
return inRequest;
}
}

Learn more on this SO question

关于objective-c - NSURLConnection 连接 : didReceiveData: is not called after redirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14216226/

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