NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", WebURL]]];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval: 30];
NSString*msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[request addValue: @"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
[request addValue:@"IOS App (power by elliott)" forHTTPHeaderField:@"User-Agent"];
[request addValue:soapActionURL forHTTPHeaderField:@"SOAPAction"];
[request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// Make sure to set the responseSerializer correctly
//operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
//
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)operation.response;
if (httpResponse.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:responseObject];
//[xmlParser setShouldProcessNamespaces:NO];
//[xmlParser setShouldReportNamespacePrefixes:NO];
//[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser setDelegate:myhttp];
[xmlParser parse];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLog(@"errror: %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
if (_delegate && [_delegate respondsToSelector:@selector(updateVCwithNetError: method:)]) {
[_delegate updateVCwithNetError:@"请求出错" method:myhttp.soapMethod];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
}];
本文介绍了一个使用 Objective-C 编写的 iOS 应用如何通过 AFNetworking 库发起 SOAP 协议的 HTTP POST 请求,并解析返回的 XML 数据。文章详细展示了设置请求头部、请求超时、请求正文编码方式以及设置请求成功和失败的回调处理过程。
3677

被折叠的 条评论
为什么被折叠?



