开发者

NSURLConnection is returning null when XML is expected

I have a basecamp account which I'm trying to access via its XML API.

I have created a NSURL Request with proper header fields.

    NSURL* projectsUrl = [NSURL URLWithString:[self.accountURL stringByAppendingString:@"/projects.xml"]];
    NSMutableURLRequest* projectsRequest = [NSMutableURLRequest requestWithURL:projectsUrl];

    [projectsRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
    [projectsRequest setValue:@"application/xml" forHTTPHeaderField:@"Accept"];

I create a connection and start it.

    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:projectsRequest delegate:self];
    [connection start];
    [connection autorelease];

Basecamp uses HTTP basic authentication. So I use a NSCredential object to handle it as below.

-(void) connecti开发者_Python百科on:(NSURLConnection*)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)authenticationChallenge
{


    if ([authenticationChallenge previousFailureCount] <=2 ) {
        NSURLCredential* basecampCredentials;
        basecampCredentials = [NSURLCredential credentialWithUser:[self username] 
                                                         password:[self password]
                                                      persistence:NSURLCredentialPersistenceNone];
        [[authenticationChallenge sender] useCredential:basecampCredentials forAuthenticationChallenge:authenticationChallenge];
    }else {
        [[authenticationChallenge sender] cancelAuthenticationChallenge:authenticationChallenge];

    }

}

And when I receive data from connection i handle it with these delegates.

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [[self responseData] appendData: data];
    NSLog(@"I received %@", [self responseData]);

}


-(void) connectionDidFinishLoading:(NSURLConnection*)connection
{
    NSLog(@"I FINALLY received %@", [self responseData]);

    NSXMLParser* parser = [[NSXMLParser alloc] initWithData:[self responseData]];
    [parser setDelegate:self];
    BOOL success = [parser parse];
    if (success) {
        NSLog(@"XML parse success");
    }else {
        NSLog(@"XML parse ERROR");
    }

    [parser autorelease];

}

The problem is even after using right urls, authentication credentials and header fields I get null data in responseData whereas when I try to access the same url with curl I get proper xml response from the basecamp server. I'm new to objective C. Please explain and tell me what else should I set to get this right.


Before starting connection ([connection start];) you should initialise property responseData, for example, self.responseData = [[NSMutableData alloc] init];.

In method - (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response you should set the length of that data to zero : [self.responseData setLength:0];


Are you sure you set [self responseData]?


Try the same URL with an ASIHTTPRequest and see if it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜