https://github.com/YouXianMing

使用 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 解析


posted @ 2014-03-05 12:29  YouXianMing  阅读(1217)  评论(0编辑  收藏  举报