如何使用 AFNetworking 或 STHTTPRequest 发出 SOAP Web 服务请求?
Posted
技术标签:
【中文标题】如何使用 AFNetworking 或 STHTTPRequest 发出 SOAP Web 服务请求?【英文标题】:How can you use AFNetworking or STHTTPRequest to make a request of a SOAP web service? 【发布时间】:2014-01-23 12:31:39 【问题描述】:我可以使用 NSMutableURLRequest
和 NSURLConnection
连接到 SOAP Web 服务,如下所示:
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">"
"<Celsius>140.0</Celsius>"
"</CelsiusToFahrenheit>"
"</soap:Body>"
"</soap:Envelope>"];
NSData *soapData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:@"www.w3schools.com" forHTTPHeaderField:@"Host"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:soapData];
我怎样才能使用AFNetworking 或STHTTPRequest 做同样的事情?
【问题讨论】:
您最初的问题本身并不是一个明确的问题,但我已对其进行了进一步的重组,因此它确实提出了一个真正的问题。结果,我认为它可以重新打开。与其说是你自己回答的问题,不如说是当你创建自我回答的问题时,问题必须是一个明确的问题。 @Brad Larson - 谢谢 【参考方案1】:STHTTPRequest
//create request
STHTTPRequest *request = [STHTTPRequest requestWithURL:url];
//set header here
[request setHeaderWithName:@"Host" value:@"www.w3schools.com"];
[request setHeaderWithName:@"SOAPAction" value: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit"];
[request setHeaderWithName:@"Content-Type" value:@"text/xml; charset=utf-8"];
//set body here
request.rawPOSTData = soapData;
//completion block
request.completionBlock = ^(NSDictionary *headers, NSString *body)
NSLog(@"headers = %@\nbody = %@", headers, body);
//parse xml string object here as request is successfull
if (body.length > 0)
NSError *error= nil;
NSDictionary *dict = [XMLReader dictionaryForXMLString:body error:&error];
if (!error)
NSLog(@"XML Dictionary: %@",dict);
//do necessary requirement here
else
NSLog(@"Error while parsing xml data : %@",[error description]);
else
NSLog(@"No response from request");
;
//error block
request.errorBlock = ^(NSError *error)
NSLog(@"%@",[error description]);
;
//start request
[request startAsynchronous];
AFNetworking
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
//parse NSXMLParser object here if request successfull
if ([responseObject isKindOfClass:[NSXMLParser class]])
NSXMLParser *parser = (NSXMLParser *)responseObject;
NSDictionary *dict = [XMLReader dictionaryForNSXMLParser:parser];
NSLog(@"JSON: %@ : %@", responseObject,dict);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
[[NSOperationQueue mainQueue] addOperation:operation];
这里XMLReader 使用NSXMLParser
提供NSDictionary
的XMLData
我在XMLReader
类中又添加了一种方法:
+(NSDictionary*)dictionaryForNSXMLParser:(NSXMLParser*)parser error:(NSError **)error
编辑:方法说明
+ (NSDictionary *)dictionaryForNSXMLParser:(NSXMLParser *)xmlParser error:(NSError **)error
XMLReader *reader = [[XMLReader alloc] initWithError:error];
NSDictionary *rootDictionary = [reader objectWithNSXMLParser:xmlParser options:0];
return rootDictionary;
objectWithNSXMLParser
方法。
- (NSDictionary *)objectWithNSXMLParser:(NSXMLParser *)xmlParser options:(XMLReaderOptions)options
// Clear out any old data
self.dictionaryStack = [[NSMutableArray alloc] init];
self.textInProgress = [[NSMutableString alloc] init];
// Initialize the stack with a fresh dictionary
[self.dictionaryStack addObject:[NSMutableDictionary dictionary]];
[xmlParser setShouldProcessNamespaces:(options & XMLReaderOptionsProcessNamespaces)];
[xmlParser setShouldReportNamespacePrefixes:(options & XMLReaderOptionsReportNamespacePrefixes)];
[xmlParser setShouldResolveExternalEntities:(options & XMLReaderOptionsResolveExternalEntities)];
xmlParser.delegate = self;
BOOL success = [xmlParser parse];
// Return the stack's root dictionary on success
if (success)
NSDictionary *resultDict = [self.dictionaryStack objectAtIndex:0];
return resultDict;
return nil;
【讨论】:
如果您还提供了您的方法的代码+(NSDictionary*)dictionaryForNSXMLParser:(NSXMLParser*)parser
,那将会很有用
@user623396 添加方法说明
能否请您发送 objectWithNSXMLParser 方法的实现
XMLReader.m 中第 65 行的错误访问
我总是收到内部错误,错误代码 500。但如果我使用 URLConnect 请求,相同的服务运行良好。可能是什么原因?以上是关于如何使用 AFNetworking 或 STHTTPRequest 发出 SOAP Web 服务请求?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 AFNetworking 或 STHTTPRequest 发出 SOAP Web 服务请求?
如何在巨大的 CollectionView 中使用 SDWebImage 或 AFNetworking 在磁盘上缓存图像?
如何使用 AFNetworking 2.0 检查 Wordpress 登录信息?