我有一個basecamp帳戶,我試圖通過它的XML API訪問。當預期XML時,NSURLConnection返回null
我已經創建了一個NSURL請求與正確的頭字段。
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"];
我創建一個連接並啓動它。
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:projectsRequest delegate:self];
[connection start];
[connection autorelease];
Basecamp使用HTTP基本認證。所以我使用NSCredential對象來處理它,如下所示。
-(void) connection:(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];
}
}
;當我從連接接收數據我與這些代表處理。
-(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];
}
的問題是,即使使用正確的網址,認證證書和標頭字段後,我得到了responseData
null
數據,而當我嘗試用curl
我從大本營服務器正確的XML響應訪問同一個網址。我是新來的客觀的C.請解釋並告訴我還有什麼我應該設置得到這個權利。
你開始'responseData'? – Scar
雅,初始化responseData工作。非常感謝 – Anand
wel。我的朋友,如果你覺得我的評論對你有幫助,請投票:P – Scar