ASIHTTPRequest and ASIWebPageRequest load local html file error

I am trying to use ASIWebPageRequest to load a local html file so that I can use the built-in caching in ASIWebPageRequest. I know this may seem a little pointless, however I want to use deleted images in my local files, which are updated only once a week or so. If that makes sense.

Here is what I do in the code:

//OUTPUT: file:///Users/ledixonuk/Library/Application%20Support/iPhone%20Simulator/4.3.2/Applications/FDF21331-CC80-4ECB-9A33-16AEC073D117/Documents/ItemOne.html NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:[FileSystemHelper dataFilePathInDocuments:@"ItemOne.html"]]; // Assume request is a property of our controller // First, we'll cancel any in-progress page load [[self webRequest] setDelegate:nil]; [[self webRequest] cancel]; [self setWebRequest:[ASIWebPageRequest requestWithURL:fileUrl]]; [[self webRequest] setDelegate:self]; [[self webRequest] setDidFailSelector:@selector(webPageFailed:)]; [[self webRequest] setDidFinishSelector:@selector(webPageFinished:)]; // Tell the request to replace urls in this page with local urls //[[self webRequest] setUrlReplacementMode:ASIReplaceExternalResourcesWithLocalURLs]; // Tell the request to embed external resources directly in the page [[self webRequest] setUrlReplacementMode:ASIReplaceExternalResourcesWithData]; // It is strongly recommended you use a download cache with ASIWebPageRequest // When using a cache, external resources are automatically stored in the cache // and can be pulled from the cache on subsequent page loads [[self webRequest] setDownloadCache:[ASIDownloadCache sharedCache]]; // Ask the download cache for a place to store the cached data // This is the most efficient way for an ASIWebPageRequest to store a web page [[self webRequest] setDownloadDestinationPath:[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self webRequest]]]; [[self webRequest] startAsynchronous]; 

And here is the error I get:

404 Not Found

Not found

The requested URL / Users / ledixonuk / Library / Application Support / iPhone Simulator / 4.3.2 / Applications / FDF21331-CC80-4ECB-9A33-16AEC073D117 / Documents / ItemOne.html was not found on this server.

Obviously, ASIWebPageRequest cannot find the ItemOne.html file in the application document folder, however, I double-checked and the file definitely exists.

Has anyone else had a similar problem? It drives me crazy trying to figure it out!

+4
source share
2 answers

ASIWebPageRequest is a subclass of ASIHTTPRequest, and unfortunately ASIHTTPRequest does not support file fetching: URLs are only http and https.

Basically, you have all the code you need, you just need to find a way to shift it together. You can create a subclass of ASIHTTPRequest that can upload files: URLs (essentially provide their own startAsyncronous project, which instead simply calls delegates after setting up the correct data files) or creates a subclass of ASIWebPageRequest that does this. I haven't thought about it in detail yet, so I have no idea what the best way would be.

(I think you see page 404, since ASIHTTPRequest manages the contact with the web server somewhere - maybe it is trying to extract from http://127.0.0.1/Users/ledixonuk/Library/ ....)

+1
source

The following is a simple implementation that I added to my ASIHTTPRequest subclass to support file-based URLs: //. This is by no means a complete and perhaps the absence of some properties that should be set, and it will not call all delegates, but for my purposes it was enough.

 - (void)startRequest { if ([url isFileURL]) { // ASIHTTPRequest does not support handling file:// URLs, this is my own simple implementation here if ([self isCancelled]) { return; } [self performSelectorOnMainThread:@selector(requestStarted) withObject:nil waitUntilDone:[NSThread isMainThread]]; NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; NSString *filePath = [url path]; BOOL isDirectory = NO; if ([fileManager fileExistsAtPath:filePath isDirectory:&isDirectory] && !isDirectory) { responseStatusCode = 200; [self setRawResponseData:[NSData dataWithContentsOfFile:filePath]]; [self setContentLength:rawResponseData.length]; [self setTotalBytesRead:[self contentLength]]; } else { responseStatusCode = 404; [self setContentLength:0]; [self setError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Cannot open file at path '%@'",filePath],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; } complete = YES; downloadComplete = YES; [self requestFinished]; [self markAsFinished]; } else { // let the original implementation deal with all the other URLs [super startRequest]; } } 
0
source

All Articles