Cocoa基本HTTP身份验证:需要建议

时间:2010-03-18 15:55:16

标签: cocoa http basic-authentication

我希望阅读使用用户名和密码保护的网页内容。这是一个mac OS X应用程序而不是iphone应用程序,因此我在这里阅读或建议阅读的大部分内容似乎都不起作用。

我也是Xcode和Obj C的初学者。我被告知要查看一个向http auth提供示例代码的网站,但到目前为止我还没什么好运这个。

下面是我的应用程序中按下按钮的主要代码,下面还有另一个名为Base64的单元,其中有一些代码,我必须更改甚至让它编译(不知道我改变了什么是正确的)

NSURL *url = [NSURL URLWithString:@"my URL"];  
NSString *userName = @"UN";  
NSString *password = @"PW";  

NSError *myError = nil;  

// create a plaintext string in the format username:password  
NSMutableString *loginString = (NSMutableString*)[@"" stringByAppendingFormat:@"%@:%@", userName, password];  

// employ the Base64 encoding above to encode the authentication tokens  
char *encodedLoginData = [base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]];  

// create the contents of the header   
NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", [NSString stringWithCString:encodedLoginData length:strlen(encodedLoginData)]];  
//NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", loginString];//[NSString stringWithString:loginString length:strlen(loginString)]]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 3];     

// add the header to the request.  Here's the $$$!!!  
[request addValue:authHeader forHTTPHeaderField:@"Authorization"];  

// perform the reqeust  
NSURLResponse *response;  

NSData *data = [NSURLConnection    
    sendSynchronousRequest: request    
    returningResponse: &response    
    error: &myError];    
//*error = myError;  

// POW, here's the content of the webserver's response.  
NSString *result = [NSString stringWithCString:[data bytes] length:[data length]];
[myTextView setString:result];
来自BASE64文件的

代码

#import "base64.h"

static char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";  

@implementation Base64  
    +(char *)encode:(NSData *)plainText {  
        // create an adequately sized buffer for the output.  every 3 bytes   
        // become four basically with padding to the next largest integer  
        // divisible by four.   
        char * encodedText = malloc((((([plainText length] % 3) +  
            [plainText length]) / 3) * 4) + 1);  
        char* inputBuffer = malloc([plainText length]);  
        inputBuffer = (char *)[plainText bytes];  

        int i;  
        int j = 0;  

        // encode, this expands every 3 bytes to 4  
        for(i = 0; i < [plainText length]; i += 3) {  
            encodedText[j++] = alphabet[(inputBuffer[i] & 0xFC) >> 2];  
            encodedText[j++] = alphabet[((inputBuffer[i] & 0x03) << 4)  
                | ((inputBuffer[i + 1] & 0xF0) >> 4)];  

            if(i + 1 >= [plainText length])  
                // padding  
                encodedText[j++] = '=';  
            else   
                encodedText[j++] = alphabet[((inputBuffer[i + 1] & 0x0F) << 2)  
                | ((inputBuffer[i + 2] & 0xC0) >> 6)];  

            if(i + 2 >= [plainText length])  
                // padding  
                encodedText[j++] = '=';  
            else  
                encodedText[j++] = alphabet[inputBuffer[i + 2] & 0x3F];  
        }  

        // terminate the string  
        encodedText[j] = 0;  

        return encodedText;//outputBuffer;  
    }  
@end  

执行代码时,它会在EXC_BAD_ACCESS的下一行停止?!?!?

NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@",   
    [NSString stringWithCString:encodedLoginData length:strlen(encodedLoginData)]]; 

任何帮助都会受到赞赏,因为我对这个问题有点无能为力,对Cocoa来说不是很有文化,客观c,xcode只为我加油。

1 个答案:

答案 0 :(得分:2)

我的iPhone书的第6章有一个关于http身份验证的例子(相同的代码可以在桌面上运行)。

您可以从此处下载示例代码:

http://objective-d.com/iphonebook

干杯,

d