discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Basic HTTP Authentication with Objective-C


From: Andreas Höschler
Subject: Re: Basic HTTP Authentication with Objective-C
Date: Tue, 13 Aug 2013 20:07:36 +0200

Hi Niels,

thanks a lot for your response:

>> I need an Objective-C (GNUstep) wrapper for accessing the OpenStreetMap API, 
>> basically an Objective-C replacement for 
> 
> I'd go with NSURLConnection and a NSMutableURLRequest that you set the 
> authentication header using -setValue:forHTTPHeaderField:. If it's not in 
> your GNUstep tree, that would indicate that it is pretty ancient, svn tells 
> me that it been there since 2006, so it should really work. I think you 
> should also be able to use the NSURLAuthenticationChallenge/NSURLCredential 
> stuff, but I've not used it on GNUstep recently.

I was wrong regarding my GNUstep tree. It already has the necessary classes. 

- (void)sendRequest:(NSString *)request method:(NSString *)method 
body:(NSString *)body
{
   NSString *urlString = [NSString 
stringWithFormat:@"http://api.openstreetmap.org%@";, request];   
   NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL 
URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy 
timeoutInterval:60.0];
   
   if (method)
     {
      NSString *authStr = [NSString stringWithFormat:@"%@:%@", username, 
password];
      NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
      NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData 
base64EncodingWithLineLength:80]];   
      [urlRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
      
      [urlRequest setHTTPMethod:method];      
     }
      
   if (body)
     {
      [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
     }
   
   _connection = [[NSURLConnection alloc] initWithRequest:urlRequest 
delegate:self];
   if (_connection)
     {
      if (_receivedData) [_receivedData setLength:0];
      else _receivedData = [[NSMutableData alloc] init];
     }
   else
     {
      NSLog(@"Creating connection failed!");
     }     
}

Works like a charm! 

Thanks,

 Andreas








reply via email to

[Prev in Thread] Current Thread [Next in Thread]