gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:52:30 28 4
gpt4 key购买 nike

我正在使用 NSURLConnection 通过 GET 请求与服务器连接。要登录,我将发送带有表单 url 的 GET 请求

http://<myUserName>:<myPassword>@my.serverurl.com/user/login

示例 URL 为

http://someusername:somepass@www.google.com/user/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];
}

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

@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];

到代码。但无论有没有这个问题仍然存在。那么对于导致此问题的原因还有其他建议吗?

最佳答案

尝试使用这个initWithURL:cachePolicy:timeoutInterval: 并将缓存策略设置为NSURLRequestReloadIgnoringCacheData

尝试添加此委托(delegate)方法。

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

确保调用此方法。

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

Please check this answer as well.

希望有效

关于iphone - NSURLConnection,使用错误的凭据登录成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135577/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com