AFHTTPSessionManager使用基于SOAP的服务

时间:2015-10-28 11:32:32

标签: ios objective-c soap afnetworking afhttpsessionmanager

我在Stackoverflow中检查了几篇帖子(thisthis),但没有回答&#34;如何使用private $data = array(); public function setData($property, $value) { $this->data[$property] = $value; } public function getData($property) { return $this->data[$property]; } 来使用基于SOAP的Web服务&#34;。< / p>

是不可能还是我们需要像子类化等那样进行一些调整?

我可以使用AFHTTPSessionManager调用SOAP,但这是我不想使用的,因为我有XML&amp;我正在使用的{JSON Web服务AFHTTPRequestOperation。我想在整个申请过程中采用类似的方法。

我使用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与上面的代码段相同),requestdatanil有一些值,response也有一些值。

error

1 个答案:

答案 0 :(得分: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];