How can you use AFNetworking or STHTTPRequest to make a request of a SOAP web service?
//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];
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];
Here XMLReader provides NSDictionary
of XMLData
using NSXMLParser
I haved added one more method in XMLReader
classes :
+(NSDictionary*)dictionaryForNSXMLParser:(NSXMLParser*)parser error:(NSError **)error
EDIT : Method Description
+ (NSDictionary *)dictionaryForNSXMLParser:(NSXMLParser *)xmlParser error:(NSError **)error
{
XMLReader *reader = [[XMLReader alloc] initWithError:error];
NSDictionary *rootDictionary = [reader objectWithNSXMLParser:xmlParser options:0];
return rootDictionary;
}
objectWithNSXMLParser
method.
- (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;
}
Paresh Navadiya
• Proficient with Objective-C, Swift and Node JS. • Experience with AWS serverless platform. • Experience with Cocoa Touch framework. • Experience on web service integration (SOAP, REST, JSON, XML). • Good understanding of OO programming and design patterns like MVC, MVVM and VIPER. • Experience with GCD, Multithreading and Operation Queue. • Experience with offline storage, threading, and performance tuning. • Understanding of Apple’s design principles and interface guidelines. • Knowledge of low-level C-based libraries. • Excellent debugging and optimization skills. • Proficient understanding of code versioning tools.
Updated on June 16, 2022Comments
-
Paresh Navadiya almost 2 years
I am able to use an
NSMutableURLRequest
with anNSURLConnection
to connect to a SOAP web service, as follows: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];
How could I do the same using AFNetworking or STHTTPRequest?
-
user623396 almost 10 yearsWould have been useful if you provided also the code for your method
+(NSDictionary*)dictionaryForNSXMLParser:(NSXMLParser*)parser
-
Paresh Navadiya almost 10 years@user623396 Method description added
-
Goran Žuri over 9 yearsCould you please send the implementation for objectWithNSXMLParser method
-
Akshit Zaveri over 9 yearsBad access on line 65 in XMLReader.m
-
Bagusflyer almost 9 yearsI always get internal error, error code 500. But the same service is running fine if I'm using URLConnect to request. What could be the cause?
-
Can almost 9 years@PareshNavadiya Thanks a lot pal!
-
Vishal's p about 7 years@PareshNavadiya In case STHTTPRequest, how Can we set value for "Authorization" in Header?
-
Paresh Navadiya about 7 yearsSame as setting header for example like [request setHeaderWithName:@"Authorization" value:@"Bearer [Access token here]"];