0

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

我试图#在最后追加,但这似乎没有奏效。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];
4

3 回答 3

3

我认为您想清除先前请求的缓存,并且每次都想在登录时进行新请求。因此,只需在请求登录之前输入以下代码

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

可以帮助你

快乐编码:)

于 2012-06-19T11:22:52.873 回答
2

我让它工作。我NSURLCredentialPersistenceSession改为NSURLCredentialPersistenceNone.

    NSURLCredential *cred = [[[NSURLCredential alloc] initWithUser:user.username password:user.password
                                                       persistence:NSURLCredentialPersistenceNone] autorelease];
于 2012-06-19T11:38:06.277 回答
0

您是否尝试过清除 Authentication 标头NSURLRequest

[urlRequest setValue:@"" forHTTPHeaderField:@"Authorization"];
于 2012-06-19T11:05:39.653 回答