3

I'm using AFNetworking to make POST requests from a shared "authenticator" class that passes in a user's username and password. Here is the POST request that I make:

 NSURL *url = [NSURL URLWithString:@"https://www..."];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

// params
NSDictionary* dict = @{@"loginName": username,
@"password": password,
@"serviceName": @"...",
@"serviceURL": @"...",
@"action": @"..."};

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request = [httpClient requestWithMethod:@"POST" path:@"..." parameters:dict];
request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
request.timeoutInterval = 30.0;

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", operation.responseString);
}
                                 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                     NSLog(@"login failed");
                                 }];

[operation start];

It works very well for the first login and everything returns as expected. When I attempt to login with a different username/password, I see that the output of the operation.responseString is the exact same output as the first login.

Does anyone know why it is returning the output from the first login? I feel that the response is a cached response and I had added the following to try to prevent the return of cached information:

request.cachePolicy = NSURLRequestReloadIgnoringCacheData;

I have set breakpoints to see that the username and password in the NSDictionary for the parameters are the new username/password combination.

The string literals are not manipulated in anyway as well and are the same in every POST request. The elipses are for privacy and are placeholders for strings with semantic meaning.

Bio
  • 505
  • 4
  • 10

3 Answers3

4

Try instead

request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

because the NSURLRequestReloadIgnoringCacheData only ignores local cache data and not caches out on the network.

Edit: As Steve Madsen points out below, this was not the real problem, and, in general, responses to POST requests are not cached in any case. The actual problem was that the program didn't log out between two logins, by mistake. But we did fix it in the end!

emrys57
  • 6,679
  • 3
  • 39
  • 49
  • I'm still getting back the same result. This seemed very promising, I am still confused as to why it didn't solve my problem! – Bio Dec 09 '12 at 21:10
  • You don't show the details of setting up your dictionary. Are you sure that the dictionary has been properly updated between the two calls? `NSLog` the dictionary just before you make the POST? – emrys57 Dec 09 '12 at 22:24
  • On second thoughts: the completion blocks you show have "success" meaning the login was ok, and "failure" meaning the login failed. But surely that's not what success and failure mean here. "Success" is just getting a sensible reply from the server. "Failure" would be, for example, A DNS lookup fail or a timeout. ???? – emrys57 Dec 09 '12 at 22:47
  • I have added the `NSLog` after the params of the dict are set and it prints out the new username and the new password. `NSLog(@" %@", [dict objectForKey:@"loginName"]);` `NSLog(@" %@", [dict objectForKey:@"password"]);` You're right that the `NSLog(@"login failed");` doesn't have the correct semantic meaning in this case but in this problem, it is not relevant. The success completion block is always executed, just with a `operation.responseString` that has the response for the first login – Bio Dec 09 '12 at 23:24
  • 1
    Do you actually log out between these two calls to the server? – emrys57 Dec 10 '12 at 07:38
  • Made a POST request to log out of the service, and subsequent logins worked as expected. Thank you! – Bio Dec 10 '12 at 09:42
  • Since this is the accepted answer, it should be edited to reflect that the bug was actually in the server. It was returning an HTTP success status and the old credentials. Worth noting: POST requests, generally speaking, should not be cached; see http://stackoverflow.com/questions/626057/is-it-possible-to-cache-post-methods-in-http. – Steve Madsen Feb 14 '13 at 22:33
1

I have same problem, and fixed finally. Using the method:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
                                                           diskCapacity:0
                                                               diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

from this blog post :How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained

pyanfield
  • 479
  • 6
  • 19
-1

Try it [request setHTTPShouldHandleCookies:NO];