使用 STHTTPRequest 框架解析 Soap1.2 教程


1.
STHTTPRequest框架地址

https://github.com/nst/STHTTPRequest

将 STHTTPRequest .h  STHTTPRequest.m 文件拖入工程中引入头文件即可

 

2.Ono框架(解析XML用)

https://github.com/mattt/Ono

将 Ono 文件夹拖入到工程中引入头文件,链接到 libxml2.dylib ,在 Header Search Path 中 添加 /usr/include/libxml2

 

   -------------------------------------------------------------------------------------------------------

   以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>
-------------------------------------------------------------------------------------------------------

复制代码
    //前期信息的准备
    //请将XXXXXXXXXXX换成自己的电话号码,否则请求会失败
    NSString *msgLength = [NSString stringWithFormat:@"%d",[@"XXXXXXXXXXX" length]];
    NSString *serviceAddress = @"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
    NSString *soapMsg = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<soap12:Envelope "
                         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                         "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
                         "<soap12:Body>"
                         "<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
                         "<mobileCode>%@</mobileCode>"
                         "<userID>%@</userID>"
                         "</getMobileCodeInfo>"
                         "</soap12:Body>"
                         "</soap12:Envelope>", @"XXXXXXXXXXX", @""];
    NSMutableDictionary *requestHeadersDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                              @"application/soap+xml; charset=utf-8", @"Content-Type" ,
                                              msgLength, @"Content-Length",
                                              nil];
    
    //组织请求内容
    STHTTPRequest *request = [STHTTPRequest requestWithURLString:serviceAddress];
    request.HTTPMethod = @"POST";
    request.requestHeaders = requestHeadersDic;
    request.rawPOSTData = [soapMsg dataUsingEncoding:NSUTF8StringEncoding];

    //注册block回调
    request.completionBlock = ^(NSDictionary *headers, NSString *body) {
        NSLog(@"%@", headers);
    };
    request.errorBlock = ^(NSError *error) {
        NSLog(@"%@", error);
    };
    request.downloadProgressBlock = ^(NSData *data, NSInteger totalBytesReceived, NSInteger totalBytesExpectedToReceive) {
        NSLog(@"%ld", (long)totalBytesReceived);
    };
    request.completionDataBlock = ^(NSDictionary *headers, NSData *body) {
        ONOXMLDocument *document = [ONOXMLDocument XMLDocumentWithData:body error:nil];
        NSLog(@"%@", document);
    };
    
    //开始异步请求
    [request startAsynchronous];
复制代码

 

分析:
请求分为3个部分:
1.请求的地址 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx ,此部分会被Request自动解析出 Host Post 地址
2.请求的头部 Content-Type Content-Length
3.请求的实体 (转换成了二进制文件)
4.得到的数据进行 XML 或者 JSON 解析
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值