iOS webservice整理
2016-04-08 11:13
337 查看
服务器采用的是webservice的方式,这种数据要请求下来是比较麻烦的,总体思路是把带有参数的XML数据拼接成字符串作为请求的Body,然后按照常规的网络请求数据的方法把数据拿下来,此时获取的数据也是XML格式的,需要对其进行解析获取对应标签对应的数据,这个数据才是最终我们需要的数据。
请求参数:(蓝色的地方是放参数的)
返回参数:
下边是请求数据的代码:
#import "ViewController.h"
#import "XMLDictionary.h"
@interface ViewController ()
@property (nonatomic,strong)NSMutableData *webData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *soapMsg = [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://www.w3.org/2003/05/soap-envelope\">"
"<soap:Body>"
"<CheckUserLogin xmlns=\"http://tempuri.org/\">"
"<resultType>%@</resultType>"
"<userName>%@</userName>"
"<password>%@</password>"
"</CheckUserLogin>"
"</soap:Body>"
"</soap:Envelope>", @"json",@"System",@"abc"];
//创建URL
NSURL *url = [NSURL URLWithString:@"http://192.168.0.254:82/PhoneService.asmx"];
NSURL *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMsg length]];
//根据URL创建一个请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//添加请求的详细信息,与请求报文前半部分对应
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//设置请求方式、
[request setHTTPMethod:@"POST"];
//将soap请求加到消息中
[request setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding ]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *str =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSDictionary dictionaryWithXMLString:str];
NSLog(@"====%@",dict);
}];
[dataTask resume ];
// Do any additional setup after loading the view, typically from a nib.
}
@end
请求参数:(蓝色的地方是放参数的)
返回参数:
下边是请求数据的代码:
#import "ViewController.h"
#import "XMLDictionary.h"
@interface ViewController ()
@property (nonatomic,strong)NSMutableData *webData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *soapMsg = [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://www.w3.org/2003/05/soap-envelope\">"
"<soap:Body>"
"<CheckUserLogin xmlns=\"http://tempuri.org/\">"
"<resultType>%@</resultType>"
"<userName>%@</userName>"
"<password>%@</password>"
"</CheckUserLogin>"
"</soap:Body>"
"</soap:Envelope>", @"json",@"System",@"abc"];
//创建URL
NSURL *url = [NSURL URLWithString:@"http://192.168.0.254:82/PhoneService.asmx"];
NSURL *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMsg length]];
//根据URL创建一个请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//添加请求的详细信息,与请求报文前半部分对应
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//设置请求方式、
[request setHTTPMethod:@"POST"];
//将soap请求加到消息中
[request setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding ]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *str =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSDictionary dictionaryWithXMLString:str];
NSLog(@"====%@",dict);
}];
[dataTask resume ];
// Do any additional setup after loading the view, typically from a nib.
}
@end
相关文章推荐
- iOS开发系列--IOS程序开发概览
- 邓白氏编码(duns number)申请入口的路径-苹果开发者申请必
- 今天开始IOS开发, 三天热度
- iOS--viewController
- iOS推送处理
- iOS--(转)集成银联3.3.0
- iOS--(转)集成银联3.3.0
- IOS-TextField控件详解
- iOS image和string的相互转换
- 苹果IOS开发者账号总结
- ios runtime基础知识
- IOS小知识
- iOS crash 崩溃问题的定位和追踪方法
- IOS自动布局中的浮动布局(6)----MyFloatLayout横空出世
- iOS 数组去重, 数组倒叙排列
- ios企业版证书怎样配置plist文件里边的url
- iOS学习笔记(十七)——文件操作(NSFileManager)
- iOS开发的交互界面的搭建选择
- iOS开发~iOS9状态栏颜色设置
- iOS开发~设置导航条颜色,导航条标题颜色、字体大小以及导航条返回按钮及其他Item颜色