您的位置:首页 > 移动开发 > IOS开发

iOS --调用webservice接口

2016-12-12 21:05 239 查看


其实小兵今天写这个东西,主要是为了以后的小伙伴遇到调用WebService接口,主要介绍了如何用IOS调用WebService(SOAP接口),需要的朋友可以参考下,在一次项目开发过程中,用到IOS调用WebService接口,所以抽个空把这方面的内容给大家整理出来,分享给大家。

Web Service基本概念

Web Service也叫XML Web Service WebService,是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。

Soap:(Simple Object Access Protocol)简单对象存取协议。是XML Web Service 的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。

XML Web Service的特点

Web Service的主要目标是跨平台的可互操作性。为了实现这一目标,Web Service 完全基于XML(可扩展标记语言)、XSD(XML Schema)等独立于平台、独立于软件供应商的标准,是创建可互操作的、分布式应用程序的新平台。

因此使用Web Service有许多优点:

1、跨防火墙的通信

2、应用程序集成

3、B2B的集成

4、软件和数据重用



Firefox浏览器中输入webService接口地址结构截图.png

主要代码:

/**
*  增加企业信息注册号、企业名称、企业类型、法人、资本总额、注册日期、住所、
期限自、期限止、营业范围、登记机关、核准日期、状态
测试

*
*  @param sender
*/
- (IBAction)Addnewbusinessinfo:(id)sender {

NSString *webServiceBodyStr = [NSString stringWithFormat:
@"<Addnewbusinessinfo xmlns=\"http://tempuri.org/\">"
"<registerNum>%@</registerNum>"
"<Enterprisename>%@</Enterprisename>"
"<Typeofenterprise>%@</Typeofenterprise>"
"<Legalrepresentative>%@</Legalrepresentative>"
"<capital>%f</capital>"
"<dateofestablishment>%@</dateofestablishment>"
"<domicile>%@</domicile>"
"<DateB>%@</DateB>"
"<DateE>%@</DateE>"
"<Businessscope>%@</Businessscope>"
"<registrationauthority>%@</registrationauthority>"
"<Approveddate>%@</Approveddate>"
"<state>%d</state>"
"</Addnewbusinessinfo>",
@"1111",@"****",@"民营",@"张三",1234.1,
@"2011-12-25T23:21:20",@"亦庄",
@"2011-12-25T23:21:20",@"2011-12-25T23:21:20",
@"建筑",@"北京 亦庄 张三",@"2011-12-25T23:21:20",true];//这里是参数

NSString *webServiceStr = [NSString stringWithFormat:
@"<?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"
"%@\n"
"</soap:Body>\n"
"</soap:Envelope>",
webServiceBodyStr];//webService头

NSString *path = @"http://168.168.5.120:8888/seal.asmx?op=Addnewbusinessinfo";
NSURL *url = [NSURL URLWithString:path];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

NSString *msgLength = [NSString stringWithFormat:@"%ld", webServiceStr.length];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"];
//    [theRequest addValue:SOAPActionStr forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[webServiceStr dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sharedSession];
//    NSURLSessionTask *task = [session dataTaskWithURL:theRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

//    }];

NSURLSessionTask *task = [session dataTaskWithRequest:theRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSLog(@"%@-",response);
NSLog(@"%@---askl",error);
NSLog(@"%@ -- data",data);

//系统自带的
NSXMLParser *par = [[NSXMLParser alloc] initWithData:data];
[par setDelegate:self];//设置NSXMLParser对象的解析方法代理
[par parse];//调用代理解析NSXMLParser对象,看解析是否成功

}];

[task resume];

}

.net后台webservice接收参数的问题

//datetime 参数,参数值拼接@“T”
"<time>%@</time>"     @"2011-12-25T23:21:20"

//blob 参数
"<state>%@</state>"     true

/**
*  转码   //base64Encoded 参数,(传入图片,NSData类型)
"<img>%@</img>"      [self base64EncodedPathForResource:@"a4"];
*
*  @param aPathForResource <#aPathForResource description#>
*
*  @return <#return value description#>
*/

//
//    // Create NSData object
//    NSData *nsdata = http://www.jianshu.com/p/[@"123456" //                      dataUsingEncoding:NSUTF8StringEncoding];
//
//    // Get NSString from NSData object in Base64
//    NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0];
//
//    // Print the Base64 encoded string
//    NSLog(@"Encoded: %@", base64Encoded);
//
//    // Let's go the other way...
//
//    // NSData from the Base64 encoded str
//    NSData *nsdataFromBase64String = [[NSData alloc]
//                                      initWithBase64EncodedString:base64Encoded options:0];
//
//    // Decoded NSString from the NSData
//    NSString *base64Decoded = [[NSString alloc]
//                               initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding];
//    NSLog(@"Decoded: %@", base64Decoded);
//
- (NSString *)base64EncodedPathForResource:(NSString *)aPathForResource
{
NSString *imgPath = [[NSBundle mainBundle] pathForResource:aPathForResource ofType:@"jpg"];
NSData *data = http://www.jianshu.com/p/[NSData dataWithContentsOfFile:imgPath];
NSString *base64Encoded = [data base64EncodedStringWithOptions:0];

return base64Encoded;

}

随手点个喜欢吧~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: