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