I am using NSURLConnection to connect with a server, using a GET request. To login I am sending a GET request with url of the form
http://<myUserName>:<myPassword>@my.serverurl.com/user/login
an example URL will be
http://someusername:somepass@www.google.com/user/login
When I login for the first time, and I pass the correct username and password, I am able to successfully login, and if I pass incorrect username or password I get a response from server saying my username and password is incorrect.. All is well then
But when I login with correct username/password, get into my home page. There I logout to go back to login screen. Now I enter incorrect username and password and try to login, I am getting response login is successfull..This is my problem.
That means all my subsequent login requests return successfull with incorrect username and password. I checked the same url (correct and incorrect) with the browser and there everything is working as it should be.
This is the code I am using to login
-(void)postRequest{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:
@"http://myusername:mypass@my.serverurl.com/user/login"]];
[request setHTTPMethod:@"GET"];
// [request setHTTPShouldHandleCookies:NO]; ==> I tried without this first
loginRequestConnection = [[CustomURLConnection alloc]
initCustomURLWithRequest:request
delegate:self
withTAG:LOGIN_REQUEST_CONNECTION];
[request release];
}
Now CustomURLConnection is nothing but a subclass of NSURLConnection and added a tag to identify between multiple connection requests in the delegate
@interface CustomURLConnection : NSURLConnection{
}
@property()NSInteger tag;
@property(nonatomic, retain) NSMutableData *receivedData;
-(id)initCustomURLWithRequest:(NSURLRequest *)request delegate:(id)delegate withTAG:(NSInteger)tagInt;
@end
I think the problem is NSURLConnection request is storing the cookie somewhere and reusing them to subsequent requests. That is why I added
[request setHTTPShouldHandleCookies:NO];
to the code. But with or without that problem persist. So any other suggestion to what is causing this problem?
try using this
initWithURL:cachePolicy:timeoutInterval:and setcache policytoNSURLRequestReloadIgnoringCacheDataTry adding this delegate method.
Ensure that this method is called.
Use
NSURLCredentialStorageandremoveCredentialto erase your cache for credentials that are stored byNSURLConnection.Please check this answer as well.
Hope that works