iOS开发 WebService 调用SOAP接口

公司后台使用.net写的小伙伴可能会遇到这个问题了
整理了几种方法
1、原生方法通过SOAP调用WebService
http://www.jianshu.com/p/acff1af35610

2、使用WSDL2ObjC工具,将接口转成OC类。

1.在浏览器中输入webService接口地址,如:http://xxx.xxx.asmx, 地址后面添加上.wsdl成http://xxx.xxx.asmx.wsdl打开。
2.将页面另存为wsdl文件,保存的时候后缀加上.wsdl,保存成如xxxxService.asmx.xml.wsdl。
3.使用WSDL2ObjC工具将wsdl文件转成OC类。
4.将生成的OC类的全部文件导入到项目中。

- (NSString *)skHkshListOfpagenow2:(NSInteger)aPagenow pagesize:(NSInteger)aPagesize { 
MURPXzshServiceSoapBinding *binding = [[MURPXzshServiceSoapBinding alloc]initWithAddress:[NSString stringWithFormat:@"%@%@", self.jjjj, XZSH_SERVICE]];
//接口地址 
binding.logXMLInOut = YES;
//可以直接copy 
MURPXzshService_HkshList *parm = [[MURPXzshService_HkshList alloc] init];
//接口中方法初始化 
parm.xxxx = [umcid stringValue];
//接口中方法传参数 
parm.pagenow = [NSNumber numberWithInteger:aPagenow];
//接口中方法传参数 
parm.pagesize = [NSNumber numberWithInteger:aPagesize];
//接口中方法传参数 
MURPXzshServiceSoapBindingResponse *resp = [binding HkshListUsingParameters:parm];
//调用方法 
//下面是返回值 
for (id mine in resp.bodyParts) { 
    if ([mine isKindOfClass:[MURPXzshService_HkshListResponse class]]) {               
          NSString *resultStr = [mine HkshListResult]; return resultStr; 
     } 
} 
    return nil;
}

3、用AFNetWorking实现请求

NSString *soapMessage =  
    [NSString stringWithFormat:  
     @""  
     ""  
     ""  
     ""  
     "%i"  
     "%@"  
     "%@"  
     ""  
     ""  
     "", par1, par2, par3  
     ];  
NSURL *url = [NSURL URLWithString:@"http://....asmx"];  
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
  
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];  
  
    [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
    [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];  
  
    [request setHTTPMethod:@"POST"];  
    [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];  
  
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];  
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
        if([self.delegate respondsToSelector:@selector(myAppHTTPClientDelegate:didUpdateWithWeather:)]){  
            [self.delegate myAppHTTPClientDelegate:self didUpdateWithWeather:responseObject];  
        }           
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {              
        if([self.delegate respondsToSelector:@selector(myAppHTTPClientDelegate:self:didFailWithError:)]){  
            [self.delegate myAppHTTPClientDelegate:self didFailWithError:error];  
        }  
    }];  
  
    [operation start];  

#use AFHTTPSessionManager

NSDictionary *s_request = @{@"par1": [NSString stringWithFormat:@"%i", par1], @"par2": par2, @"par3": par3, @"par4": [NSString stringWithFormat:@"%i", par4], @"par5": par5};  
  
AFHTTPSessionManager* manager = [[AFHTTPSessionManager alloc] init];   

[manager POST:@"http://192.168.10.26/mywebservice/myservice.asmx?op=MethodName" parameters:request success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  NSLog(@"DONE!");  
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
    NSLog(@"Failed to log in: %@", operation.responseString);  
}];

4、就是使用第三方了

你可能感兴趣的:(iOS开发 WebService 调用SOAP接口)