【发布时间】:2016-01-28 03:51:42
【问题描述】:
我查看了 Stackoverflow 中的几篇帖子(this 和 this),但没有一篇回答“如何使用 AFHTTPSessionManager 使用基于 SOAP 的 Web 服务”。
这是不可能的,还是我们需要做一些调整,比如子类化等?
我可以使用AFHTTPRequestOperation 调用基于SOAP 的方法,但这是我不想使用的,因为我在那里有XML 和JSON Web 服务,我正在使用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 与上面的代码 sn-p 相同),我得到 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);
}];
【问题讨论】:
-
检查您的内容类型为 [request setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
-
@Anbu.Karthik 上面的代码有效,但它使用了
RequestOpeation,我想用AFNetworking类的SessionManager实现同样的效果。 -
没有前辈,我也试了1个多小时也没得到答案
标签: ios objective-c soap afnetworking afhttpsessionmanager