iOS webservice整理

服务器采用的是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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值