iOS中得基于SOAP协议进行数据请求方式我也是头一次用,现在就这个数据请求方式做一些总结。
以下我借鉴了cocoa china mousefly的文章并进行整理。
mark一下:iOS项目中后台如果用.net写的话,这篇文章可能对你有很大的帮助。
webServiceFile:(
*) wsFile
xmlNameSpace:(
*) xmlNS
webServiceName:(
*) wsName
+ (
*)getSOAP11WebServiceResponse:(
*) WebURL
*) wsFile
*) xmlNS
*) wsName
*) wsParas;
*)getSOAP11WebServiceResponseWithNTLM:(
*) WebURL
*) wsFile
*) xmlNS
*) wsName
*) wsParas
userName:(
*) userName
passWord:(
*) passWord;
*)checkResponseError:(
2.将以下代码的实现写到.m文件中
*) theResponse;
/*
//Mark: 生成SOAP1.1版本的ASIHttp请求
参数 webURL: 远程WebService的地址,不含*.asmx
参数 webServiceFile: 远程WebService的访问文件名,如service.asmx
参数 xmlNS: 远程WebService的命名空间
参数 webServiceName: 远程WebService的名称
*/
*) 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];
* soapParas = [[
alloc] init];
int
i = 0;
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]];
*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]];
[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的名称
*/
*) WebURL
*) wsFile
*) xmlNS
*) wsName
*) wsParas
{
//创建请求
ASIHTTPRequest * theRequest = [
@H_404_45@self
getASISOAP11Request:WebURL
webServiceFile:wsFile
xmlNameSpace:xmlNS
webServiceName:wsName
//显示网络请求信息在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是否包含错误信息
如果未包含错误,则返回零长度字符串
否则返回错误描述
*/
*) theResponse
{
//检查消息是否包含错误前缀
(![theResponse hasPrefix:[Constant sharedConstant].G_WEBSERVICE_ERROR]) {
;
}
NSMutableString
*stemp = [[
alloc] initWithString:theResponse];
NSRange
range=[stemp rangeOfString:[Constant sharedConstant].G_WEBSERVICE_ERROR];
//剔除错误前缀
* errMsg = stemp;
//Authentication needed
}
//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?)"
]) {
}
//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@}