0
votes

I need to extract some data from a http header. Prior to being sent by the server, the data has been base64 encoded and then URL encoded. The header looks like:

<snip>
Server = Apache;
"Transfer-Encoding" = Identity;
"Www-Authenticate" = "Basic realm=\"itYNcEpMfSPfewXAOte3II6xXsM6aNBO197bBuvb9gvWVl7Xo%2FQJ9j9r0hHz0k12xLRqlyvczoCM7kI9q1opHj%2BKYiPz73DqypNFgYGleR3n0bcVTto80Hq55i6nsgPaCnHrWJOdQs1HY%2FzzuK6vbZYAIofiB7VKSwdi00ZmkbQi9Pi05i4lCaCu%2FwV%2FXOOS95oL8TQ%3D\"";

In order to remove the URL encoding and base64 encoding, should I extract the raw header data as an NSString or as NSData?

NSString* option1 = [header objectForKey: @"Www-Authenticate"];
NSData* option2 = [header objectForKey: @"Www-Authenticate"];

Is it important to choose one over the other, or could both options be used equally?

(Note I'm not asking how to decode from URL encoding and decode from Base64, I'm asking if the starting point should be as NSString or NSData and why, or it doesn't matter).

2

2 Answers

0
votes

base64 this is string, which you can convert to NSData.

NSString* option1 = [header objectForKey: @"Www-Authenticate"];
NSData *data = [NSData dataFromBase64String:option1];
-2
votes
- (NSString*)encodeTo64:(NSString*)fromString
{
    NSData *plainData = [fromString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String;
    if ([plainData respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
        base64String = [plainData base64EncodedStringWithOptions:kNilOptions];  // iOS 7+
    } else {
        base64String = [plainData base64Encoding];                              // pre iOS7
    }


    return base64String;
}