AFHTTPSessionManager to use SOAP based service

孤街醉人 提交于 2019-12-02 06:35:33

问题


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);
}];

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!