美文网首页
webservice

webservice

作者: b2e16cc43137 | 来源:发表于2016-06-13 10:57 被阅读147次

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

                        @""

                        "

                        "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\">"

                        ""

                        ""

                        "%@"

                        "%@"

                        "%@"

                        ""

                        ""

                        "", @"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

相关文章

网友评论

      本文标题:webservice

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