iOS +AFNetworking2.0调用webService

本文章采用的字符串常量:

  1. NSString *soapMessage =  
  2. @"<?xml version=\"1.0\" encoding=\"utf-8\"?> \n"  
  3. "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"  
  4. "<soap12:Body>"  
  5. "<getSupportCity xmlns=\"http://WebXml.com.cn/\">"  
  6. "<byProvinceName>ALL</byProvinceName>"  
  7. "</getSupportCity>"  
  8. "</soap12:Body>"  
  9. "</soap12:Envelope>";  
  10. NSString *soapLength = [NSString stringWithFormat:@"%d", [soapMessage length]];  

 

之前写的代码有问题,因为webxml这个网站支持GET、POST和SOAP协议,所以在接收到回执的时候误以为是SOAP请求成功,但仔细看了一下返回信息发现是普通的POST请求成功的消息。以下是错误的代码

AFNetworking 2.0

  1. AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.webxml.com.cn"]];  
  2. manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];  
  3. [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:@"WebServices/WeatherWebService.asmx/getSupportCity" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {  
  4.     [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"application/x-www-form-urlencoded", @"Content-Type", soapLength, @"Content-Length", nil] body:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];  
  5. }];  
  6.   
  7. [manager POST:@"/WebServices/WeatherWebService.asmx/getSupportCity" parameters:@{@"byProvinceName":@"ALL"} success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  8.     NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];  
  9.     NSLog(@"responseObject: %@", response);  
  10. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  11.     NSLog(@"Error: %@", error);  
  12. }];  

 

在发现错误后,本人调试跟踪了一下AFNetworking2.0的POST过程,发现在添加Content-Type和Content-Length的时候,AFNetworking把字段值改掉了,所以在AFHTTPRequestOperationManager类中添加了如下方法:

  1. - (AFHTTPRequestOperation *)SOAP:(NSString *)URLString  
  2.        constructingBodyWithBlock:(void (^)(NSMutableURLRequest *request))block  
  3.                          success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success  
  4.                          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure  
  5. {  
  6.     NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:nil constructingBodyWithBlock:nil];  
  7.     block(request);  
  8.     AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];  
  9.     [self.operationQueue addOperation:operation];  
  10.       
  11.     return operation;  
  12. }  

 
添加好此方法后,直接调用就可以了

  1. AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.webxml.com.cn"]];  
  2. manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];  
  3. [manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
  4. [manager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"];  
  5. [manager SOAP:@"/WebServices/WeatherWebService.asmx" constructingBodyWithBlock:^(NSMutableURLRequest *request) {  
  6.     [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];  
  7. } success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  8.     NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];  
  9.     NSLog(@"%@, %@", operation, response);  
  10. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  11.     NSString *response = [[NSString alloc] initWithData:(NSData *)[operation responseObject] encoding:NSUTF8StringEncoding];  
  12.     NSLog(@"%@, %@", operation, error);  
  13. }];  

 
AFNetworking2.0 + SOAP调用成功!!!

 

闲着没事把之前的代码拿来又看了一遍,想着能不能在不改动AFNetworking代码的前提下调用SOAP协议成功,如果有同样想法的朋友可以试一试下面的代码

  1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
  2. manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];  
  3. [manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
  4. [manager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"];  
  5. NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" parameters:nil];  
  6. [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];  
  7. AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  8.     NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];  
  9.     NSLog(@"%@, %@", operation, response);  
  10. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  11.     NSString *response = [[NSString alloc] initWithData:(NSData *)[operation responseObject] encoding:NSUTF8StringEncoding];  
  12.     NSLog(@"%@, %@", operation, error);  
  13. }];  
  14. [manager.operationQueue addOperation:operation];}  

 
调用成功!

  1. ASIHTTPRequest  
  2. NSString *soapMessage =  
  3. @"<?xml version=\"1.0\" encoding=\"utf-8\"?> \n"  
  4. "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"  
  5. "<soap12:Body>"  
  6. "<getSupportCity xmlns=\"http://WebXml.com.cn/\">"  
  7. "<byProvinceName>ALL</byProvinceName>"  
  8. "</getSupportCity>"  
  9. "</soap12:Body>"  
  10. "</soap12:Envelope>";  
  11. NSString *soapLength = [NSString stringWithFormat:@"%d", [soapMessage length]];  
  12.   
  13. NSURL *url = [NSURL URLWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"];  
  14. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  15. [request setDelegate:self];  
  16.   
  17. [request addRequestHeader:@"Content-Type" value:@"application/soap+xml; charset=utf-8"];  
  18. [request addRequestHeader:@"Content-Length" value:soapLength];  
  19. [request setRequestMethod:@"POST"];  
  20. [request appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];  
  21. [request setDefaultResponseEncoding:NSUTF8StringEncoding];  
  22. [request startAsynchronous];  



转自:http://jiapumin.iteye.com/blog/2109378





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值