基于SOAP协议并使用ASIHTTPRequest调用Webservice完成数据请求。

        iOS中得基于SOAP协议进行数据请求方式我也是头一次用,现在就这个数据请求方式做一些总结。

        以下我借鉴了cocoa china mousefly的文章并进行整理。

        mark一下:iOS项目中后台如果用.net写的话,这篇文章可能对你有很大的帮助。

        1.将以下代码封装成一个数据访问类的.h中

+ (ASIHTTPRequest *)getASISOAP11Request:( NSString *) WebURL
                          webServiceFile:( NSString *) wsFile
                            xmlNameSpace:( NSString *) xmlNS
                          webServiceName:( NSString *) wsName
                            wsParameters:( NSMutableArray *) wsParas;
 
+ ( NSString *)getSOAP11WebServiceResponse:( NSString *) WebURL
                            webServiceFile:( NSString *) wsFile
                              xmlNameSpace:( NSString *) xmlNS
                            webServiceName:( NSString *) wsName
                              wsParameters:( NSMutableArray *) wsParas;
 
+ ( NSString *)getSOAP11WebServiceResponseWithNTLM:( NSString *) WebURL
                                    webServiceFile:( NSString *) wsFile
                                      xmlNameSpace:( NSString *) xmlNS
                                    webServiceName:( NSString *) wsName
                                      wsParameters:( NSMutableArray *) wsParas
                                         userName:( NSString *) userName
                                          passWord:( NSString *) passWord;
 
+ ( NSString *)checkResponseError:( NSString *) theResponse;
        2.将以下代码的实现写到.m文件中

        

/*
//Mark: 生成SOAP1.1版本的ASIHttp请求
  参数 webURL:                远程WebService的地址,不含*.asmx
  参数 webServiceFile:        远程WebService的访问文件名,如service.asmx
  参数 xmlNS:                    远程WebService的命名空间
  参数 webServiceName:        远程WebService的名称
  参数 wsParameters:            调用参数数组,形式为[参数1名称,参数1值,参数2名称,参数2值⋯⋯],如果没有调用参数,此参数为nil
  */
+ (ASIHTTPRequest *)getASISOAP11Request:( NSString *) WebURL
                          webServiceFile:( NSString *) wsFile
                            xmlNameSpace:( NSString *) xmlNS
                          webServiceName:( NSString *) wsName
                            wsParameters:( NSMutableArray *) wsParas
{
     //1、初始化SOAP消息体
     NSString * soapMsgBody1 = [[ NSString alloc] initWithFormat:
                                @ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
                                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
                                "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                                "<soap:Body>\n"
                                "<%@ xmlns=\"%@\">\n" , wsName, xmlNS];
     NSString * soapMsgBody2 = [[ NSString alloc] initWithFormat:
                                @ "</%@>\n"
                                "</soap:Body>\n"
                                "</soap:Envelope>" , wsName];
     
     //2、生成SOAP调用参数
     NSString * soapParas = [[ NSString alloc] init];
     soapParas = @ "" ;
     if (![wsParas isEqual: nil ]) {
         int i = 0;
         for (i = 0; i < [wsParas count]; i = i + 2) {
             soapParas = [soapParas stringByAppendingFormat:@ "<%@>%@</%@>\n" ,
                          [wsParas objectAtIndex:i],
                          [wsParas objectAtIndex:i+1],
                          [wsParas objectAtIndex:i]];
         }
     }
     
     //3、生成SOAP消息
     NSString * soapMsg = [soapMsgBody1 stringByAppendingFormat:@ "%@%@" , soapParas, soapMsgBody2];
     
     //请求发送到的路径
     NSURL * url = [ NSURL URLWithString:[ NSString stringWithFormat:@ "%@%@" , WebURL, wsFile]];
     
     //NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
     ASIHTTPRequest * theRequest = [ASIHTTPRequest requestWithURL:url];
     NSString *msgLength = [ NSString stringWithFormat:@ "%d" , [soapMsg length]];
     
     //以下对请求信息添加属性前四句是必有的,第五句是soap信息。
     [theRequest addRequestHeader:@ "Content-Type" value:@ "text/xml; charset=utf-8" ];
     [theRequest addRequestHeader:@ "SOAPAction" value:[ NSString stringWithFormat:@ "%@%@" , xmlNS, wsName]];
     
     [theRequest addRequestHeader:@ "Content-Length" value:msgLength];
     [theRequest setRequestMethod:@ "POST" ];
     [theRequest appendPostData:[soapMsg dataUsingEncoding: NSUTF8StringEncoding ]];
     
     [theRequest setDefaultResponseEncoding: NSUTF8StringEncoding ];
     
     return theRequest;
}



#pragma mark -
/*
  //Mark: 使用SOAP1.1同步调用WebService请求
  参数 webURL:                远程WebService的地址,不含*.asmx
  参数 webServiceFile:        远程WebService的访问文件名,如service.asmx
  参数 xmlNS:                    远程WebService的命名空间
  参数 webServiceName:        远程WebService的名称
  参数 wsParameters:            调用参数数组,形式为[参数1名称,参数1值,参数2名称,参数2值⋯⋯],如果没有调用参数,此参数为nil
  */
+ ( NSString *)getSOAP11WebServiceResponse:( NSString *) WebURL
                            webServiceFile:( NSString *) wsFile
                              xmlNameSpace:( NSString *) xmlNS
                            webServiceName:( NSString *) wsName
                              wsParameters:( NSMutableArray *) wsParas
{
     //创建请求
     ASIHTTPRequest * theRequest = [ self getASISOAP11Request:WebURL
                                              webServiceFile:wsFile
                                                xmlNameSpace:xmlNS
                                              webServiceName:wsName
                                                wsParameters:wsParas];
     
     //显示网络请求信息在status bar上
     [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: YES ];
     
     //同步调用
     [theRequest startSynchronous];
     NSError *error = [theRequest error];
     if (!error) {
         return [theRequest responseString];
     }
     else {
         //出现调用错误,则使用错误前缀+错误描述
         return [ NSString stringWithFormat:@ "%@%@" , [Constant sharedConstant].G_WEBSERVICE_ERROR, [error localizedDescription]];
     }
}


       3.错误信息

#pragma mark -
/*
  //Mark: 检查WebService的Response是否包含错误信息
     如果未包含错误,则返回零长度字符串
     否则返回错误描述
     错误信息格式:错误前缀\n错误描述
  */
+ ( NSString *)checkResponseError:( NSString *) theResponse
{
     //检查消息是否包含错误前缀
     if (![theResponse hasPrefix:[Constant sharedConstant].G_WEBSERVICE_ERROR]) {
         return @ "" ;
     }
     else {
         NSMutableString *sTemp = [[ NSMutableString alloc] initWithString:theResponse];
         //获取错误前缀的范围
         NSRange range=[sTemp rangeOfString:[Constant sharedConstant].G_WEBSERVICE_ERROR];
         //剔除错误前缀
         [sTemp replaceCharactersInRange:range withString:@ "" ];
         
         NSString * errMsg = sTemp;
         //Authentication needed
         if ([sTemp isEqualToString:@ "Authentication needed" ]) {
             errMsg = @ "用户登录失败!" ;
         }
         //The request timed out
         if ([sTemp isEqualToString:@ "The request timed out" ]) {
             errMsg = @ "访问超时,请检查远程地址等基本设置!" ;
         }
         //The request was cancelled
         if ([sTemp isEqualToString:@ "The request was cancelled" ]) {
             errMsg = @ "请求被撤销!" ;
         }
         //Unable to create request (bad url?)
         if ([sTemp isEqualToString:@ "Unable to create request (bad url?)" ]) {
             errMsg = @ "无法创建请求,错误的URL地址!" ;
         }
         //The request failed because it redirected too many times
         if ([sTemp isEqualToString:@ "The request failed because it redirected too many times" ]) {
             errMsg = @ "请求失败,可能是因为被重定向次数过多!" ;
         }
         //A connection failure occurred
         if ([sTemp isEqualToString:@ "A connection failure occurred" ]) {
             errMsg = @ "网络连接错误,请检查无线或3G网络设置!" ;
         }
         
         return errMsg;
     }
}

        4.开始调用

  //创建WebService的调用参数
     NSMutableArray * wsParas = [[ NSMutableArray alloc] initWithObjects:
                                 @ "UserName" , [Constant sharedConstant].P_USER_NAME,
                                 nil ];
     
     //调用WebService,获取响应
     NSString * theResponse = [WebService getSOAP11WebServiceResponseWithNTLM:[Constant sharedConstant].P_SYSTEM_URL        
                                                               webServiceFile:[Constant sharedConstant].G_WS_TODOCENTER
                                                                 xmlNameSpace:[Constant sharedConstant].G_WEBSERVICE_NAMESPACE
                                                               webServiceName:[Constant sharedConstant].G_WS_TODOCENTER_GETWORKFLOWTODOCOUNT
                                                                 wsParameters:wsParas
                                                                     userName:[Constant sharedConstant].P_USER_NAME
                                                                     passWord:[Constant sharedConstant].P_PASSWORD];
     
     //检查响应中是否包含错误
     NSString * errMsg = [WebService checkResponseError:theResponse];
     
     //接下来的代码就是检查errMsg有没有内容
     //再接下来就是theResponse响应字符串的解析了
接下来对相应的字符串进行解析就可以了。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值