2012-06-19 52 views
0

我遇到了一个小问题。我正在连接到使用基本身份验证的web服务,现在我希望用户能够:注销,或每次请求进行一次新的身份验证。现在认证似乎被缓存了。我该怎么做呢?iPhone上的基本身份验证注销

我试图在最后追加#,但这似乎没有办法。该URL的格式如下:

www.example.org/mywebservice/data

NSURL* url = [NSURL URLWithString: @"www.example.org/mywebservice/data"]; 
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url]; 
NSURLConnection* newConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
self.myConnection = newConnection; 

[request release]; 
[newConnection release]; 
+0

我们可以看到您如何处理与Web服务的连接?你在使用NSUrlConnection吗? – self

+0

是的我使用NSURLConnection – LuckyLuke

+1

你看这个:http://www.springenwerk.com/2008/11/i-am-currently-building-iphone.html – self

回答

2

我得到它的工作。我将NSURLCredentialPersistenceSession更改为NSURLCredentialPersistenceNone

NSURLCredential *cred = [[[NSURLCredential alloc] initWithUser:user.username password:user.password 
                 persistence:NSURLCredentialPersistenceNone] autorelease]; 
0

您是否尝试过在你的NSURLRequest清除认证头?

[urlRequest setValue:@"" forHTTPHeaderField:@"Authorization"]; 
+0

对我不起作用 –

3

我想你想清除先前请求的缓存,并且每次都希望在登录时做新的请求。因此,对于刚刚把下面的代码做请求之前登录

NSHTTPCookie *cookie; 
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 
    for (cookie in [storage cookies]) 
    { 
     [storage deleteCookie:cookie]; 
    } 
    [[NSURLCache sharedURLCache] removeAllCachedResponses]; 

可以帮你

编码快乐:)

+0

我不得不说这解决了我在使用旧版AFNetworking库的遗留应用程序中使用身份验证令牌缓存时遇到的问题。 –