NSURLConnection,使用错误的凭据登录成功

时间:2012-04-13 05:14:28

标签: iphone objective-c cookies login nsurlconnection

我使用NSURLConnection使用GET请求连接服务器。要登录,我发送一个GET请求,其中包含

形式的网址
http://<myUserName>:<myPassword>@my.serverurl.com/user/login

示例网址为

http://someusername:somepass@www.google.com/user/login 

当我第一次登录时,我传递了正确的用户名和密码,我能够成功登录,如果我传递错误的用户名或密码,我会收到服务器的回复,说我的用户名和密码不正确。一切都很好

但是当我使用正确的用户名/密码登录时,请进入我的主页。在那里我退出登录屏幕。现在我输入错误的用户名和密码并尝试登录,我收到回复登录成功。这是我的问题。

这意味着我的所有后续登录请求都会使用错误的用户名和密码返回成功。我用浏览器检查了相同的URL(正确和不正确),并且一切正常。

这是我用来登录的代码

-(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];
}

现在CustomURLConnection只是NSURLConnection的子类,并添加了一个标记来识别委托中的多个连接请求

@interface CustomURLConnection : NSURLConnection{
}
@property()NSInteger tag;
@property(nonatomic, retain) NSMutableData *receivedData;

-(id)initCustomURLWithRequest:(NSURLRequest *)request delegate:(id)delegate withTAG:(NSInteger)tagInt;
@end

我认为问题是NSURLConnection请求正在某处存储cookie并将其重用于后续请求。这就是我添加

的原因
[request setHTTPShouldHandleCookies:NO];

代码。但无论有没有这个问题仍然存在。那么对造成这个问题的原因有什么其他建议吗?

1 个答案:

答案 0 :(得分:3)

尝试使用此功能 initWithURL:cachePolicy:timeoutInterval:并将cache policy设置为NSURLRequestReloadIgnoringCacheData

尝试添加此委托方法。

 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {   
return nil; 
}

确保调用此方法。

使用NSURLCredentialStorageremoveCredential清除缓存中NSURLConnection存储的凭据。

Please check this answer as well.

希望有效