AFHTTPSessionManager使用基于SOAP的服务 [英] AFHTTPSessionManager to use SOAP based service
问题描述
我查了几篇帖子(这个和在Stackoverflow中这个)但他们都没有回答如何使用 AFHTTPSessionManager
使用基于SOAP的Web服务。
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?
我可以使用 AFHTTPRequestOperation
调用SOAP,但这是我不想使用的,因为我有XML&我在那里使用的JSON Web服务 AFHTTPSessionManager
。我想在整个申请过程中采用类似的方法。
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.
我的代码使用 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
但这不是'工作:( 请求
与上面的代码片段相同),我得到数据
为 nil
和响应
有一些值,错误
中有一些值。
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];
这篇关于AFHTTPSessionManager使用基于SOAP的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!