iPhone模拟器上的代理授权

时间:2011-03-07 10:15:52

标签: iphone http proxy authorization nsurlconnection

有这样的问题,希望你能帮助我..找不到任何地方。这是我正在使用的代码:

NSURL *serverURL = [NSURL URLWithString:@"http://someServer.com/Login"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL];
[request addValue:@"text/plain" forHTTPHeaderField:@"ACCEPT"];
[request setHTTPMethod:@"POST"];

[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];

NSData *data = [encodedString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];

URLLoader *loader = [URLLoader loaderWithRequest:request];

当我想从计算机上的浏览器访问服务器http://someServer.com/Login时,一切正常。

当我试图从iPhone模拟器上的safary访问同一台服务器时 - 授权后窗口会出现授权窗口。

但是当我在iPhone模拟器上以程序方式执行此请求时,我发布了代码示例。出现下一个错误:

The following error was encountered:
Cache Access Denied.

Sorry, you are not currently allowed to request:
   http://someserver.com/Login
from this cache until you have authenticated yourself.

我该如何解决这个问题?感谢名单!

1 个答案:

答案 0 :(得分:1)

您似乎需要在拨打电话时发送身份验证凭据。 尝试使用NSURLConnection类进行调用,设置委托并实现以下方法

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:@"ABC"
                                                 password:@"XYZ"
                                              persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];

    } else {

        [[challenge sender] cancelAuthenticationChallenge:challenge];
        // inform the user that the user name and password
        // in the preferences are incorrect
    }

}