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

发布于 2024-08-25 19:56:03 字数 3762 浏览 6 评论 0原文

我希望阅读受用户名和密码保护的网页内容。这是一个 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、objective c 不太了解,xcode 只是为我火上浇油。

im looking to read the contents of a webpage that is secured with a user name and password. this is a mac OS X application NOT an iphone app so most of the things i have read on here or been suggested to read do not seem to work.

Also i am a total beginner with Xcode and Obj C i was told to have a look at a website that provided sample code to http auth however so far i have had little luck in getting this working.

below is the main code for the button press in my application, there is also another unit called Base64 below that has some code in i had to change to even get it compiling (no idea if what i changed is correct mind you).

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];

code from the BASE64 file

#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  

when executing the code it stops on the following line with a EXC_BAD_ACCESS ?!?!?

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

any help would be appreciated as i am a little clueless on this problem, not being very literate with Cocoa, objective c, xcode is only adding fuel to this fire for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2024-09-01 19:56:03

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

您可以从这里下载示例代码:

http://objective-d.com/iphonebook

干杯,

d.

Chapter 6 of my iPhone book has an example on http authentication (the same code will work on the desktop).

You can download the sample code from here:

http://objective-d.com/iphonebook

Cheers,

d.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文