美文网首页
iOS 调用webService 接口

iOS 调用webService 接口

作者: 山杨 | 来源:发表于2016-11-15 09:49 被阅读76次

1 ) SOAP:基于XML的一种协议规范,用来描述传递信息的格式(接口调用要遵循此格式)
2 ) WSDL:描述如何调用或访问具体的接口
3 ) UDDI:管理,查询webService

注:调用接口最重要的就是要遵循SOAP协议格式

SOAP协议一般格式

<?xml version="1.0" encoding="utf-8"?>  
<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/"> 
<soap:Body>   
 //Body之间是需要调用的方法以及参数列表
 </soap:Body> 
</soap:Envelope>

发送soap请求:

//soap格式要和接口吻合
NSString *str = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\ 
<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/\">\ 
<soap:Body>\  
<要调用的方法名 xmlns=\"命名空间\">\ 
<参数>参数值</参数>\ 
...
</要调用的方法名>\
</soap:Body>\  
</soap:Envelope>"];
NSURL *url = [NSURL URLWithString:@"接口网址"];     
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];      
NSString *length = [NSString stringWithFormat:@"%lu",(unsigned long)[str length]];      
[request setHTTPMethod:@"POST"];     
[request addValue:@"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];      
[request addValue:length forHTTPHeaderField:@"Content-Length"];     
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];     // body内容    
NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {      
//请求完成是xml格式的,可以先不处理,用字符串接收查看一下          
NSString *resultStr = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];         
if (error) {              
NSLog(@"---失败----%@", error.localizedDescription);         
}     
}]; 
[task resume]; }

相关文章

网友评论

      本文标题:iOS 调用webService 接口

      本文链接:https://www.haomeiwen.com/subject/hopkpttx.html