zoukankan      html  css  js  c++  java
  • ios下通过webservice获取数据

    经历了两天的摸索,终于成功获取了数据,因为公司要做一个停车入库的信息查询,所以需要访问webservice的接口,由于没有接触过webservice,所以第一天就是各种搜索资料,类库,各种尝试,甚至是没有用soap封装请求头就直接post请求数据,结果是请求回来的xml是错误,因此,第二天开始静下心来看网上的讲解,把自己的经历及请求过程记录如下:

    主要参考如下两个资源(非常非常感谢)

    http://www.cocoachina.com/bbs/read.php?tid-98388-page-1.html(自己封装独立类调用ASI方法实现)

    http://blog.csdn.net/a6472953/article/details/9813799(最原始的经典实现方式)

    我用第一种方法实现,关于参数注释第一篇基本都已说明,需要的参数只有如下:

    #define P_SYSTEM_URL @"http://公网地址:端口/" // 不加*.asmx(*是指端口后.asmx前所有部分,这部分为第二个宏定义)
    #define G_WS_TODOCENTER @"*.asmx" // (*是指端口后.asmx前所有部分)
    #define G_WEBSERVICE_NAMESPACE @"http://tempuri.org/"  // (如果没有指定命名空间,默认为http://tempuri.org/)
    #define G_WS_TODOCENTER_GETWORKFLOWTODOCOUNT xxx  // (请求的方法名)
    //#define P_USER_NAME  // 可以不设定,代码中相关注释即可
    //#define P_PASSWORD   // 可以不设定,代码中相关注释即可

    //---------------------------------------对请求的理解------------------------------------------

    //1、初始化SOAP消息体
        NSString * soapMsgBody1 = [[NSString alloc] initWithFormat:
                                   @"<?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://schemas.xmlsoap.org/soap/envelope/"> "
                                   "<soap:Body> "
                                   "<%@ xmlns="%@"> ", wsName, xmlNS];

    NSString * soapMsgBody2 = [[NSString alloc] initWithFormat:
                                   @"</%@> "
                                   "</soap:Body> "
                                   "</soap:Envelope>", wsName];

    加粗部分为固定,需要改变的是wsName和xmlNS,wsName是webservice名即为上面定义的G_WS_TODOCENTER_GETWORKFLOWTODOCOUNT,xmlNS为命名空间即为上面定义的G_WEBSERVICE_NAMESPACE

    //2、生成SOAP调用参数
        NSString * soapParas = [[NSString alloc] init];
        soapParas = @"";
        if (![wsParas isEqual:nil]) {
            int i = 0;
            for (i = 0; i < [wsParas count]; i = i + 2) {
                soapParas = [soapParas stringByAppendingFormat:@"<%@>%@</%@> ",
                             [wsParas objectAtIndex:i],
                             [wsParas objectAtIndex:i+1],
                             [wsParas objectAtIndex:i]];
            }
        }

    //3、生成SOAP消息
        NSString * soapMsg = [soapMsgBody1 stringByAppendingFormat:@"%@%@", soapParas, soapMsgBody2];

    ASIHTTPRequest * theRequest = [ASIHTTPRequest requestWithURL:url];
        NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
        
        //以下对请求信息添加属性前四句是必有的,第五句是soap信息。
        [theRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
        [theRequest addRequestHeader:@"SOAPAction" value:[NSString stringWithFormat:@"%@%@", xmlNS, wsName]];
        
        [theRequest addRequestHeader:@"Content-Length" value:msgLength];
        [theRequest setRequestMethod:@"POST"];
        [theRequest appendPostData:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
        
        [theRequest setDefaultResponseEncoding:NSUTF8StringEncoding];
        
        return theRequest;

    // -------------自己拼接-----------

        NSString *soapMessage = [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://schemas.xmlsoap.org/soap/envelope/"> "
                                 "<soap:Body> "
                                 "<GetMyRecord xmlns="http://tempuri.org/"> "
                                 "<参数名1>参数1值</参数名1> "

              "<参数名2>参数2值</参数名2> "
                                 "</GetMyRecord> "
                                 "</soap:Body> "
                                 "</soap:Envelope> "
                                 ];

        
        NSURL *url = [NSURL URLWithString:@"请求连接(.asmx格式)"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
        
        //以下对请求信息添加属性前四句是必有的,第五句是soap信息。
        [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request addValue: @"命名空间/方法名(或叫webservice名称)" forHTTPHeaderField:@"SOAPAction"];
        
        [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
        
        //请求
    //    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            [self parserWithData:data];  // 自定义的parserWithData(XML解析方法)
        }];

  • 相关阅读:
    python全栈学习--day31(正则)
    python 全栈开发,Day26(hashlib文件一致性,configparser,logging,collections模块)
    python全栈学习--面向对象进阶3
    python全栈学习--面向对象进阶2
    面向对象进阶
    python全栈学习--Day19(面向对象组合,继承)
    python全栈学习--Day18(面向对象交互)
    python全栈学习--Day17(初识面向对象)
    python全栈学习--day15(递归函数,二分查找法)
    python-函数
  • 原文地址:https://www.cnblogs.com/MengXY/p/4368487.html
Copyright © 2011-2022 走看看