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

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

Posted on  2014-03-05 12:29  游贤明 阅读( 14) 评论( 0)  编辑  收藏

使用 STHTTPRequest 框架解析 Soap1.2 教程_第1张图片

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];

你可能感兴趣的:(使用 STHTTPRequest 框架解析 Soap1.2 教程)