【问题标题】:Basic HTTP Authentication on iPhoneiPhone 上的基本 HTTP 身份验证
【发布时间】:2009-06-14 18:23:00
【问题描述】:

我正在尝试运行一个小型 Twitter 客户端,但在测试需要身份验证的 API 调用时遇到了问题。

我的密码中有特殊字符,所以当我尝试使用以下代码时它不起作用。

NSString *post = [NSString stringWithFormat:@"status=%@", [status stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@@%@/statuses/update.json", username, password, TwitterHostname]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

我开始研究 base64 并将身份验证放入标头中。我在他的 base64 实现上找到了Dave Dribin's 帖子,这似乎很有意义。但是,当我尝试使用它时,编译器开始抱怨它如何找不到 openssl 库。所以我读到我需要链接 libcrypto 库,但它似乎不存在于 iphone 中。

我还读到有人说苹果不允许应用使用加密库,这对我来说没有意义。

所以现在我有点困惑和困惑。在我的应用中获得基本身份验证的最简单方法是什么?

干杯

【问题讨论】:

    标签: iphone objective-c httpwebrequest http-authentication


    【解决方案1】:

    两件事。首先你必须使用异步方法而不是同步/类方法。

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:req]
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:30.0];
    
    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    

    通过在您的委托中实现此方法来管理身份验证:

    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
    

    您可能还需要实现这些方法:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection;
    

    无论如何,使用异步方法往往会提供更好的用户体验,因此尽管有额外的复杂性,即使没有进行身份验证的能力也是值得的。

    【讨论】:

    • 我将 sendSynchronous 调用更改为 initWithRequest:request delegate:self,然后在委托中设置了 didReceiveAuthenticationChallenge,但没有调用任何内容。我尝试了其他 NSURLConnection 委托方法,但它们也不起作用。有什么想法吗?
    • 我弄清楚了为什么委托方法不起作用。我从一个不起作用的类方法调用 initWithRequest 。感谢您的帮助。
    • 对不起,不必要的简洁回答(但很高兴你最终到达那里)。我已经扩展了它。希望它现在更有用一点。
    【解决方案2】:

    您也可以直接在主 URL 中写入下载用户名和密码,即https://username:password@yoururl.com/

    首先你需要调用 NSURLConnection 委托文件:-

    • (BOOL)连接:(NSURLConnection *)连接可以AuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

      {

      返回是; }

    然后调用 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

    {
    if ([challenge previousFailureCount] == 0)
            {
                NSURLCredential *newCredential;
    
                newCredential = [NSURLCredential credentialWithUser:@"username"
                                                           password:@"password"
                                                        persistence:NSURLCredentialPersistenceForSession];
                NSLog(@"NEwCred:- %@ %@", newCredential, newCredential);
                [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
            }
            else
            {
                [[challenge sender] cancelAuthenticationChallenge:challenge];
                NSLog (@"failed authentication");
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 2011-11-12
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多