gpt4 book ai didi

ios - 从 http header 中删除 cookie?

转载 作者:行者123 更新时间:2023-11-29 12:23:05 43 4
gpt4 key购买 nike

我正在使用带有简单 header 身份验证的 Web 服务

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSString *userName = [_usernameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];;
NSString *passWord = [_passwordTextfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([challenge previousFailureCount] == 0) {
//Creating new credintial
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:userName
password:passWord
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
else {
CommonCode*objCommon=[[CommonCode alloc]init];
[_activityIndicator stopAnimating];
[objCommon showAlert:@"Invalid Password, or no user found with this Email Address"];
}
}

在注销时我用

清除cokies
- (void)resetCredintialCache {
NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
if ([credentialsDict count] > 0) {
// the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential
NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
id urlProtectionSpace;
// iterate over all NSURLProtectionSpaces
while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
NSEnumerator *userNameEnumerator = [credentialsDict[urlProtectionSpace] keyEnumerator];
id userName;
// iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials
while (userName = [userNameEnumerator nextObject]) {
NSURLCredential *cred = credentialsDict[urlProtectionSpace][userName];
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
}
}
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
[sharedCache removeAllCachedResponses];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *cookies = [cookieStorage cookies];
for (NSHTTPCookie *cookie in cookies) {

[cookieStorage deleteCookie:cookie];
}
}
}

但是在注销后,如果我输入错误的密码,我将以以前的用户身份登录。如何从 HTTP 请求的 header 中删除 cookie?

最佳答案

NSURLRequest有一个cachePolicy属性,指定了请求的缓存行为。

设置以下缓存策略NSURLRequestReloadIgnoringLocalCacheData 发出请求时,如下例将从 url 而不是缓存加载数据。

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];

NSURLRequestReloadIgnoringLocalCacheData

Specifies that the data for the URL load should be loaded from the originating source. No existing cache data should be used to satisfy a URL load request.

https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy

关于ios - 从 http header 中删除 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30025988/

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