iOS Developer

基于 SOAP1.2(简单对象访问协议)进行网络请求

2016-12-03  本文已影响1076人  EagleOne

什么是 SOAP(Simple Object Access Protocol)

简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息。

 AFHTTPSessionManager *manager =  [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
 
    //因为返回的不是标准的 XML 或者 JSON 格式,所以用 AFHTTPRequestSerializer
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer = serializer;
    
    //缓存策略
    serializer.cachePolicy = NSURLRequestUseProtocolCachePolicy;
    serializer.timeoutInterval = 30.0f;
    
    //设置请求头
    [serializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [serializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Accept"];
   
   //SOAP协议采用 的类似这样的请求体,这个即是下面的 SOAP 消息
   // <?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>
//    <E_GetUserInfo xmlns="http://tempuri.org/">
//    <CardNo>H13932169</CardNo>
//    </E_GetUserInfo>
//    </soap12:Body>
//    </soap12:Envelope>

    NSString* msgLength = [NSString stringWithFormat:@"%lu",soapRequestConfigureModel.SOAPMsg.length];
    
    //请求的内容长度
    [serializer setValue:msgLength forHTTPHeaderField:@"Content-Length"];
    //指定请求的服务器的域名和端口号
    [serializer setValue:@"221.224.118.58" forHTTPHeaderField:@"Host"];
    //自定义请求体 HTTPBody 这步是必须的
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return soapRequestConfigureModel.soapMsg;
    }];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //参数传 SOAPMsg
     [manager POST:BaseURL parameters:SOAPMsg progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

构建模块

语法规则

消息基本结构

<?xml
 version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
 
<soap:Header>
</soap:Header>

<soap:Body>
<soap:Fault>
</soap:Fault>
</soap:Body>

</soap:Envelope>
 <?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>
    //命令以及包含的参数
    <E_GetUserInfo xmlns="http://tempuri.org/">
    <CardNo>H13932169</CardNo>
    </E_GetUserInfo>
    
    </soap12:Body>
    </soap12:Envelope>
上一篇下一篇

猜你喜欢

热点阅读