AFHTTPSessionManager для использования службы на основе SOAP
Я проверил несколько сообщений ( это и это) в Stackru, но ни один из них не ответил "Как использовать AFHTTPSessionManager
использовать веб-сервис на основе SOAP ".
Разве это не возможно, или мы должны сделать некоторые настройки, такие как создание подклассов и т. Д.?
Я могу позвонить на основе SOAP с помощью AFHTTPRequestOperation
но это то, что я не хочу использовать, так как у меня там есть веб-сервисы XML и JSON, которые я использую AFHTTPSessionManager
, Я хочу иметь похожий подход во всем приложении.
Мои коды используют RequestOperation
является:
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];
Я пытался с помощью SessionManager
но это не работает: (request
такой же, как в приведенном выше фрагменте кода), я получаю data
как nil
а также response
имеет некоторые значения, а также error
имеет некоторые значения в нем.
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);
}];
1 ответ
Я пытался в течение нескольких часов и, наконец, смог получить ответ и без ошибок. Вот обновленный код:
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];