1

我正在尝试设置缓存,但是线程没有访问我使用的“如下”方法。

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse

我正在像这样初始化连接,并且访问了 connectionDidFinishLoading,所以我不确定我错过了什么。

- (IBAction)searchRequest:(NSData *)postBodyData
{
    //Set database address
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"https://127.0.0.1:88"]; 

    NSURL *url = [NSURL URLWithString:databaseURL];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postBodyData length]];

    //SynchronousRequest to grab the data, also setting up the cachePolicy
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; //if request dose not finish happen within 60 second timeout.

//    NSInputStream *fileStream = [NSInputStream inputStreamWithData:postBodyData];


    [request setHTTPMethod: @"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/octet-stream" forHTTPHeaderField:@"content-type"];
    [request setHTTPBody:postBodyData];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = [NSMutableData data];
    } else {
        // Inform the user that the connection failed from the connection:didFailWithError method
    }
}

任何帮助,将不胜感激。

4

1 回答 1

2

connection:willCacheResponse:仅在响应将被缓存的情况下调用。在大多数情况下,POST 请求是不可缓存的。(更多细节:是否可以在 HTTP 中缓存 POST 方法?

您可能应该看看像MKNetworkKit这样处理大量此类缓存的东西,特别是对于 REST 协议。

You can also look at Drop-in offline caching for UIWebView. You'd have to modify it significantly, but NSURLProtocol can be used to solve this kind of problem. AFCache is currently working to integrate this approach, and is another toolkit to consider. (Read through the comments in the blog post for more background on the issues.)

于 2012-03-30T01:16:13.540 回答