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.
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.
精彩评论