• iOS另一种获取soap的方法


    首先新建类WebServices

    WebServices.h

    #import <Foundation/Foundation.h>
    
    @interface WebServiceHelper : NSObject
    
    @property (nonatomic,strong) NSString *MethodName;
    
    @property (nonatomic,strong) NSString *SoapUrlAddress;
    
    @property (nonatomic,strong) NSMutableDictionary *MethodKeyAndValues;
    
    @property (nonatomic,strong) NSString *XmlNameSpace;
    
    -(NSString *)Post;
    
    @end
    

    WebServices.m

    #import "WebServiceHelper.h"
    
    @implementation WebServiceHelper
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            self.SoapUrlAddress=@"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
            
            self.XmlNameSpace=@"http://WebXml.com.cn/";
        }
        return self;
    }
    
    -(NSString *)Post
    {
        NSString *xml=[[NSString alloc]init];
        
        NSMutableString *soapMsg=[[NSMutableString alloc]init];
        [soapMsg appendFormat:@"<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" "];
        [soapMsg appendFormat:@"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>"];
        [soapMsg appendFormat:@"<%@ xmlns="%@">",self.MethodName,self.XmlNameSpace];
        
        NSArray *keys=[self.MethodKeyAndValues allKeys];
        int length=[keys count];
        
        for (int i=0; i<length; i++) {
            id key=[keys objectAtIndex:i];
            id value=[self.MethodKeyAndValues objectForKey:key];
            [soapMsg appendFormat:@"<%@>%@</%@>",key,value,key];
        }
        
        [soapMsg appendFormat:@"</%@></soap:Body></soap:Envelope>",self.MethodName];
        
        NSLog(@"%@",soapMsg);
        
        NSURL *url=[NSURL URLWithString:self.SoapUrlAddress];
        
        NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:url];
        
        [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        NSString *soapAction=[[NSString alloc]initWithFormat:@"%@%@",self.XmlNameSpace,self.MethodName];
        
        [req addValue:soapAction forHTTPHeaderField:@"SOAPAction"];
        
        NSLog(@"%@",soapAction);
        
        [req setHTTPMethod:@"POST"];
        
        [req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
        
        NSURLResponse *response=nil;
        NSError *error=nil;
        NSData *result=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
        if (error) {
            NSLog(@"Connection Error: %@", [error description], nil);
        }
        
        xml=[[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];
        return xml;
    }
    
    @end
    

    调用方法

    WebServiceHelper *helper=[[WebServiceHelper alloc]init];
        
        helper.MethodName=@"getMobileCodeInfo";
        
        NSMutableDictionary *keyAndValues=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"123456789",@"mobileCode",@"",@"userid",nil];
        
        helper.MethodKeyAndValues=keyAndValues;
        
        NSString *value=[helper Post];
        
        NSLog(@"%@",value);
    

     与前一个比的好处就是,不用委托了。因为是初学,大家多提意见。

  • 相关阅读:
    刷题-力扣-541. 反转字符串 II
    刷题-力扣-515. 在每个树行中找最大值
    刷题-力扣-513. 找树左下角的值
    刷题-力扣-404. 左叶子之和
    刷题-力扣-257. 二叉树的所有路径
    刷题-力扣-226. 翻转二叉树
    刷题-力扣-236. 二叉树的最近公共祖先
    刷题-力扣-235. 二叉搜索树的最近公共祖先
    刷题-力扣-145. 二叉树的后序遍历
    扛把子组2018092609-2 选题 Scrum立会报告+燃尽图 06
  • 原文地址:https://www.cnblogs.com/youyuan1980/p/5029468.html
Copyright © 2020-2023  润新知