NSCachedURLResponse返回对象,但UIWebView不解释内容 [英] NSCachedURLResponse returns object, but UIWebView does not interprets content
问题描述
我正在向UIWebView发送请求。加载的网页上有AJAX调用。我需要分析AJAX流量,以确定用户是否登录。为此,我在AppDelegate中安装了一个NSURLCache:
I am sending a request to a UIWebView. There are AJAX-calls on the loaded webpage. I need to analyze the AJAX-traffic in order to determinate, if the user is logged in or not. For doing this I installed a NSURLCache in the AppDelegate:
MYURLCache *cache = [[MYURLCache alloc] init];
[NSURLCache setSharedURLCache:cache];
此MYURLCache类正确接收通过webview运行的流量。我只能终止AJAX调用。然后我产生了一个自己的AJAX调用请求。这个返回来自网络服务器的完美回复。所以现在要做的最后一步是构建NSCachedURLResponse返回对象。我也管理过这个,但是webview在返回对象时什么都不做。如果我只返回nil,则WebView会加载所有内容(nil是NSURLCache的消息,没有任何内容被缓存,因此webview应该开始自己加载)。
This MYURLCache-class correctly receives the traffic that runs through the webview. I can terminate the AJAX-calls only. I then spawn an own request of the AJAX-calls. This one returns the perfectly fine response from the webserver. So the last step to do now is constructing the NSCachedURLResponse return object. I also managed this, but the webview simply does nothing when returning the objects. If I return just nil, the WebView loads everything fine (nil is the message for the NSURLCache, that nothing is cached, so the webview should start to load it on its own).
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
if ([[[request URL] absoluteString] rangeOfString:@"/ajax/"].location == NSNotFound) {
return nil;
} else {
ASIHTTPRequest *asirequest = [ASIHTTPRequest requestWithURL:[request URL]];
[asirequest setValidatesSecureCertificate:NO];
[asirequest startSynchronous];
NSError *error = [asirequest error];
NSData* data = [[asirequest responseString] dataUsingEncoding:NSUTF8StringEncoding];
// Create the cacheable response
NSURLResponse *urlresponse = [[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"application/json" expectedContentLength:[data length] textEncodingName:@"UTF-8"];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data];
NSLog(@"cachedResponse %@", cachedResponse);
NSLog(@"cachedResponse data %@", [[NSString alloc] initWithData:[cachedResponse data] encoding:NSUTF8StringEncoding]);
return cachedResponse;
}
return nil;
}
推荐答案
我找到了一个解决方案问题...我认为这与丢失的标题有关。
I found one solution to this problem... I think it has got something to do with the headers that were missing.
如果我更换
NSURLResponse *urlresponse = [[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"application/json" expectedContentLength:[data length] textEncodingName:@"UTF-8"];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data];
使用
NSHTTPURLResponse *urlresponse = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:nil];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data];
整个过程都有效。此答案另外建议了请求的附加自定义标头。 https://stackoverflow.com/a/15234850/274518
The whole thing works. This answer in addition suggested the additional custom header of the request. https://stackoverflow.com/a/15234850/274518
NSDictionary *headers = @{@"Access-Control-Allow-Origin" : @"*", @"Access-Control-Allow-Headers" : @"Content-Type"};
NSHTTPURLResponse *urlresponse = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:headers];
在我的情况下,我不需要它。
In my case I did not needed it.
这篇关于NSCachedURLResponse返回对象,但UIWebView不解释内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!