iOS -- webservice

我们经常遇到后台编写的是以 wsdl 的接口地址,遇到这样的接口我们应该怎么处理的?
首先用浏览器直接打开该地址

iOS -- webservice_第1张图片
打开地址.png

从上图中可以看出webservice请求的相关信息,这里我们需要知道targetNamespace(命名空间)

拼接含有参数的soap字符串:


+(NSString *)requestfordata:(NSArray *)data andrequestname :(NSString *)name{
    
    NSMutableData *postBody = [NSMutableData data];
    
    [postBody appendData:[[NSString stringWithFormat:@"<%@ xmlns=\"****命名空间*****\" id=\"o0\" c:root=\"1\">",name] dataUsingEncoding:NSUTF8StringEncoding]];
    for(int i =0;i", name] dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSString * xmlStr=[[NSString alloc]initWithData:postBody encoding:NSUTF8StringEncoding];
    
    NSString *str = [NSString stringWithFormat:@""
                            ""
                            """%@"
                            ""
                            "", xmlStr];
    
    return str;
}

调用网络请求:


+ (NSDictionary *)postRequestWithUrl:(NSString *)hostUrl andSoapUrl:(NSString *)url{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
    
    // 设置请求超时时间
    manager.requestSerializer.timeoutInterval = 30;
    
    // 返回NSData
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // 设置请求头,也可以不设置
    [manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", url.length] forHTTPHeaderField:@"Content-Length"];
    
    // 设置HTTPBody
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
        return url;
    }];
    
    __block NSDictionary *dict ;
    [manager POST:hostUrl parameters:url success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // 把返回的二进制数据转为字符串
        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        // 利用正则表达式取出之间的字符串
        NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:@"(?<=ns1:out\\>).*(?=

现在就能够按照正常的请求方式解析得到数据了.....

你可能感兴趣的:(iOS -- webservice)