iOS从具有身份验证的服务器读取xml数据

时间:2013-11-07 13:30:49

标签: ios xml xml-parsing

在我的应用程序中,我要从服务器读取xml数据。要访问此服务器,需要提供用户名和密码,我该如何解决? 我尝试使用以下代码读取xml数据:

-(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);
}

它连接正确,但如果我试着阅读问题,我会得到这个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>

所以我想有必要提供用户名和密码,但我该怎么做?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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];

}

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