NSURLProtocol子类会自动更改URL
我使用的是UIWebView,并且已经实现了NSURLProtocol的自己的子类来拦截响应中的PDF文件。 一切工作正常。 但是,我意识到我们的一些网站在哪里实施了重写代理。 URL正在发生变化,导致HTTP 404。
为了理解我尝试在canInitWithRequest中返回NO的情况。 那时候它工作的很完美,但是当我发送YES时,它的后续请求URL被改变了。
这是我在startLoading方法中写的
- (void)startLoading {
NSMutableURLRequest* request = self.request.mutableCopy;
NSLog(@"%@",request.URL);
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if([response isKindOfClass:[NSHTTPURLResponse class]]){
NSDictionary *aInfo = [(NSHTTPURLResponse*)response allHeaderFields];
if([(NSString*)aInfo[@"Content-Type"] containsString:@"application/pdf"]){
// Code to save DATA as PDF File
//in Document Directory via call back to other classes.
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowedInMemoryOnly];
[self.client URLProtocol:self didLoadData:nil];
[self.client URLProtocolDidFinishLoading:self];
}else{
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[self.client URLProtocol:self didLoadData:data];
[self.client URLProtocolDidFinishLoading:self];
}
aInfo = nil;
}
}];
[postDataTask resume];
}
链接地址: http://www.djcxy.com/p/34809.html
上一篇: NSURLProtocol subclass changes URLs automatically
下一篇: Why is the HTTPBody of a request inside an NSURLProtocol Subclass always nil?