加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 23:51:20 所属栏目:安全 来源:网络整理
导读:? ? ? ? iOS中得基于SOAP协议进行数据请求方式我也是头一次用,现在就这个数据请求方式做一些总结。 ? ? ? ? 以下我借鉴了cocoa china mousefly的文章并进行整理。 ? ? ? ? mark一下:iOS项目中后台如果用.net写的话,这篇文章可能对你有很大的帮助。 ? ? ?

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

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

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

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

+ (ASIHTTPRequest *)getASISOAP11Request:( NSString *) WebURL
????????????????????????? webServiceFile:( *) wsFile
??????????????????????????? xmlNameSpace:( *) xmlNS
webServiceName:( *) wsName
wsParameters:( 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>" ?????
//2、生成SOAP调用参数
* soapParas = [[ 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消息
* soapMsg = [soapMsgBody1 stringByAppendingFormat:@ "%@%@" ?????
//请求发送到的路径
NSURL * url = [ URLWithString:[ stringWithFormat:@ ?????
//NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
ASIHTTPRequest * theRequest = [ASIHTTPRequest requestWithURL:url];
*msgLength = [ "%d" ?????
//以下对请求信息添加属性前四句是必有的,第五句是soap信息。
[theRequest addRequestHeader:@ "Content-Type" value:@ "text/xml; charset=utf-8" ];
"SOAPAction" value:[ ?????
"Content-Length" value:msgLength];
[theRequest setRequestMethod:@ "POST" ];
[theRequest appendPostData:[soapMsg dataUsingEncoding: 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 = [ self getASISOAP11Request:WebURL
????????????????????????????????????????????? webServiceFile:wsFile
??????????????????????????????????????????????? xmlNameSpace:xmlNS
webServiceName:wsName
wsParameters:wsParas];
?????
//显示网络请求信息在status bar上
[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: YES ];
?????
//同步调用
[theRequest startSynchronous];
NSError *error = [theRequest error];
(!error) {
[theRequest responseString];
}
else {
???????? //出现调用错误,则使用错误前缀+错误描述
[ }
}


? ? ? ?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;
}
}

? ? ? ? 4.开始调用

//创建WebService的调用参数
* wsParas = [[ alloc] initWithObjects:
???????????????????????????????? "UserName" ???????????????????????????????? ];
?????
//调用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];
?????
//检查响应中是否包含错误
* errMsg = [WebService checkResponseError:theResponse];
?????
//接下来的代码就是检查errMsg有没有内容
//再接下来就是theResponse响应字符串的解析了
接下来对相应的字符串进行解析就可以了。。。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读