I checked few posts (this and this) in Stackoverflow but none of them answered "How to use AFHTTPSessionManager
to use SOAP based web service".
Is it not possible or we need to do some tweaking like subclassing etc?
I can call the SOAP based using AFHTTPRequestOperation
but this is what I don't want to use since I have XML & JSON web services there I am using AFHTTPSessionManager
. I want to have similar approach throughout my application.
My codes using RequestOperation
is :
NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
"<Celsius>50</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>n";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://tempuri.org/"]];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"responseObject = %@", [operation responseString]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error = %@", error);
}];
[operation start];
I tried using SessionManager
but this doesn't work: (the request
is same as in above code snippet), I get data
as nil
and response
has some values, also the error
has some values in it.
AFHTTPSessionManager *sManager = [[AFHTTPSessionManager alloc] init];
[sManager dataTaskWithRequest:(NSURLRequest *)request
completionHandler:^( NSURLResponse *response, NSData *data, NSError *error){
NSLog(@"Data: %@", data);
NSLog(@"Resp: %@", response);
NSLog(@"Err: %@", error);
}];
I tried for multiple hours and finally was able to get some response and no-error. Here is the updated code:
NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
"<Celsius>50</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>n";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://tempuri.org/"]];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPSessionManager *sManager = [[AFHTTPSessionManager alloc] init];
sManager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionDataTask *dataTask = [sManager dataTaskWithRequest:(NSURLRequest *)request
completionHandler:^( NSURLResponse *response, id responseObject, NSError *error){
NSString *resString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@", resString);
//NSLog(@"Resp: %@", [response ]);
NSLog(@"Err: %@", error);
}];
[dataTask resume];
来源:https://stackoverflow.com/questions/33389940/afhttpsessionmanager-to-use-soap-based-service