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

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