iOS から .NET Web サービスに正常に接続し、応答 xml ドキュメントを NSLog 経由でコンソールに出力しています。これは、AFNetworking ライブラリを介して行っています。NSURLConnection 経由でこの同じ Web サービスに接続できるようになりましたが、NSMutableData オブジェクト内に保持されている xml ドキュメントを抽出するのに問題があります。AFNetworking ライブラリはこれを自動的に行うことができるため、作業がずっと簡単になりますが、ネイティブの iOS ライブラリを使用してこれを行う方法を知りたいです。ここに私が取り組んでいるコードがあります:
//This is the code I am using to make the connection to the web service
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[request addValue:@"http://tempuri.org/Customers" forHTTPHeaderField:@"SOAPAction"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
// Convert your data and set your request's HTTPBody property
NSString *stringData = @"some data";
NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = requestBodyData;
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
Web サービスからデータを受信するために必要なコードは次のとおりです。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
NSString *strData = [[NSString alloc]initWithData:_responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", strData);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
}
/*
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", [operation responseString]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failed");
}];
[operation start];
*/
受け取った xml ドキュメント全体を印刷したいだけで、今のところドキュメントを解析する必要はありません。
回答者全員に事前に感謝します。