In my app I've to read xml data from a server. To access to this server it's necessary to give an username and a password, how I can solve that? I tried to read xml data with this code:
-(id)sendRequestToURL:(NSString*)url withMethod:(NSString*)method {
NSURL *finalUrl;
if ([method isEqualToString:@"GET"]) {
finalUrl = [NSURL URLWithString:url];
} else {
NSLog(@"Metodo non implementato");
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalUrl];
[request setHTTPMethod:method];
[request setValue:@"x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-type"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
[connection start];
}
return connection;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Ho ricevuto una risposta");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Ho ricevuto dei dati: %@", data);
NSMutableData *test = [[NSMutableData alloc]init];
[test appendData:data];
NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Ho terminato di caricare");
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@", error);
}
It connect correctly but if I try to read what's the problem I'm getting this HTML:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at 54.204.6.246 Port 80</address>
</body></html>
So I guess that it's necessary to give username and password but how I can do that?