1

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?

4

2 回答 2

0

我以这种方式解决了这个问题:

    NSString *address = [NSString stringWithFormat:@"http://54.204.6.246/magento8/api/rest/products/?category_id=3"];
    [self sendRequestToURL:address withMethod:@"GET"];

    -(id)sendRequestToURL:(NSString*)url withMethod:(NSString*)method {
    NSURL *finalUrl;
    if ([method isEqualToString:@"GET"]) {
        finalUrl = [NSURL URLWithString:url];
    } else {
        NSLog(@"Metodo non previsto");
    }
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalUrl];
    [request setHTTPMethod:method];
    NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"user", @"password"];
    NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];
    [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);
    jsonCategory = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Ho terminato di caricare");
    JsonCategoryReader *reader = [[JsonCategoryReader alloc]init];
    [reader parseJson:jsonCategory];

}

我希望它对其他有同样问题的人有用。(注意代码,它只是一个片段,可能不是所有的 {})

于 2013-11-07T15:04:42.483 回答
0

base64EncodedString 设置认证头域:

NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"myusername", @"mypassword"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

要将 NSData 转换为 base64 字符串,您需要NSData+Base64.h

于 2013-11-07T13:32:53.090 回答