微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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

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

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

        

/*
//Mark: 生成SOAP1.1版本的ASIHttp请求
  参数 webURL:                远程WebService的地址,不含*.asmx
参数 webServiceFile:        远程WebService的访问文件名,如service.asmx
参数 xmlNS:                    远程WebService的命名空间
参数 webServiceName:        远程WebService的名称
参数 wsParameters:            调用参数数组,形式为[参数1名称,参数1值,参数2名称,参数2值⋯⋯],如果没有调用参数,此参数为nil
*/
*) WebURL
*) wsFile
*) xmlNS
*) wsName
*) wsParas
{
     //1、初始化SOAP消息体
     * soapMsgBody1 = [[ 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];
* soapMsgBody2 = [[ alloc] initWithFormat:
"</%@>\n"
"</soap:Body>\n"
"</soap:Envelope>" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,wsName];
     
//2、生成SOAP调用参数
* soapParas = [[ alloc] init];
     soapParas = @ "" ;
if (![wsParas isEqual: @H_404_45@nil ]) {
         int i = 0;
         @H_404_45@for (i = 0; i < [wsParas count]; i = i + 2) {
             soapParas = [soapParas stringByAppendingFormat:@ "<%@>%@</%@>\n" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
[wsParas objectAtIndex:i],
[wsParas objectAtIndex:i+1],
[wsParas objectAtIndex:i]];
         }
}
     
//3、生成SOAP消息
* soapMsg = [soapMsgBody1 stringByAppendingFormat:@ "%@%@" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,soapParas,soapMsgBody2];
     
//请求发送到的路径
NSURL * url = [ URLWithString:[ stringWithFormat:@ ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,WebURL,wsFile]];
     
//NSMutableuRLRequest *theRequest = [NSMutableuRLRequest requestWithURL:url];
@H_502_322@ASIHTTPRequest * theRequest = [ASIHTTPRequest requestWithURL:url];
*msgLength = [ "%d" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,[soapMsg length]];
     
//以下对请求信息添加属性前四句是必有的,第五句是soap信息。
[theRequest addRequestHeader:@ "Content-Type" value:@ "text/xml; charset=utf-8" ];
"SOAPAction" value:[ ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,xmlNS,wsName]];
     
"Content-Length" value:msgLength];
[theRequest setRequestMethod:@ "POST" ];
[theRequest appendPostData:[soapMsg dataUsingEncoding: @H_404_45@NSUTF8StringEncoding ]];
     
[theRequest setDefaultResponseEncoding: ];
     
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
*/
*) WebURL
*) wsFile
*) xmlNS
*) wsName
*) wsParas
{
//创建请求
ASIHTTPRequest * theRequest = [ @H_404_45@self getASISOAP11Request:WebURL
                                              webServiceFile:wsFile
                                                xmlNameSpace:xmlNS
webServiceName:wsName
wsParameters:wsParas];
     
//显示网络请求信息在status bar上
[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: @H_404_45@YES ];
     
//同步调用
[theRequest startSynchronous];
NSError *error = [theRequest error];
(!error) {
[theRequest responseString];
}
else {
         //出现调用错误,则使用错误前缀+错误描述
[ ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,[Constant sharedConstant].G_WEBSERVICE_ERROR,[error localizedDescription]];
}
}


       3.错误信息

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

        4.开始调用

//创建WebService的调用参数
* wsParas = [[ alloc] initWithObjects:
                                 "UserName" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,[Constant sharedConstant].P_USER_NAME,
                                 ];
     
//调用WebService,获取响应
* 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];
     
@H_502_777@//检查响应中是否包含错误
* errMsg = [WebService checkResponseError:theResponse];
     
//接下来的代码就是检查errMsg有没有内容
//再接下来就是theResponse响应字符串的解析了
接下来对相应的字符串进行解析就可以了。。。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐