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];
}
の問題は、私は私がベースキャンプサーバから適切なXML応答を取得curl
と同じURLにアクセスしようとするのに対し、responseData
でnull
データを取得しても、右のURL、認証資格情報とヘッダフィールドを使用した後です。私は客観的なCには新しいです。この権利を得るために他に何を設定すべきか説明して教えてください。
あなたは 'responseData'を開始しましたか? – Scar
ya、responseDataを初期化しました。おかげでたくさん – Anand
笑。私の友人、あなたが私のコメントがあなたを助けたと感じたら、投票してください:P – Scar