IOS调用webService函数获取数据的相关问题
本帖最后由 bixuemeigui 于 2012-11-30 18:12:37 编辑 开发一个IOS应用程序:需要调用WebService函数取得数据库中的数据:
数据查询接口函数为:String getReportMassage ( String startStatTime, String endStatTime, String operator, String password,String type);
下面是使用ASIHTTPRequest类调用WebService函数:
//生成soap消息函数getASISOAP11Request
-(NSString*)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];
NSLog(@"%@",soapMsg);
return soapMsg;
}
//发送Soap消息
-(void)ServiceRequestUrl:(NSString*)WebURL ServiceMethodName:(NSString*)strMethod SoapMessage:(NSString*)soapMsg{
//请求发送到的路径
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", WebURL]];
//NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
//[theRequest setURL:url];
theRequest= [ASIHTTPRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
//以下对请求信息添加属性前四句是必有的,第五句是soap信息。
[theRequest addRequestHeader:@"Host" value:[url host]];
[theRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
[theRequest addRequestHeader:@"Content-Length" value:msgLength];
[theRequest addRequestHeader:@"SOAPAction" value:[NSString stringWithFormat:@"%@%@",defaultWebServiceNameSpace,strMethod]];
[theRequest setRequestMethod:@"POST"];
//传soap信息
[theRequest appendPostData:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setValidatesSecureCertificate:NO];
//[theRequest setTimeOutSeconds:60.0];
[theRequest setDefaultResponseEncoding:NSUTF8StringEncoding];
//return theRequest;
}
#pragma -
#pragma 同步请求
//返回数据
-(NSString*)SysServiceUrl:(NSString*)strUrl ServiceMethodName:(NSString*)strMethod SoapMessage:(NSString*)soap
{
ASIHTTPRequest *request=[self requestServiceUrl:strUrl ServiceMethodName:strMethod SoapMessage:soap];
[request startSynchronous];
NSError *error=[request error];
int statusCode = [request responseStatusCode];
NSLog(@"%d",statusCode);
if (error||statusCode!=200) {
return @"";
}
return [SoapXmlParseHelper SoapMessageResultXml:[request responseString] ServiceMethodName:strMethod];
}
//发送请求,获取数据
- (void)sender: (id) sender {
NSString *startStatTime =[NSString stringWithFormat:@"20121120"];
NSString *endStatTime =[NSString stringWithFormat:@"20121130"];
NSString *Operator =[NSString stringWithFormat:@"800001"];
NSString *password =[NSString stringWithFormat:@"E10ADC3949BA59ABBE56E057F20F883E"];
NSString *type =[NSString stringWithFormat:@"01"];
NSMutableArray *paras=[[NSMutableArray alloc]
initWithObjects:@"startStatTime",startStatTime,@"endStatTime",endStatTime,@"operator" ,Operator,@"password",password,@"type",type,nil];
//调用函数
NSString *soapMsg =[self getASISOAP11Request:@"http://192.168.86.230:8088/dvteboss_ws/wsservice/" webServiceFile:@"ReportWS" xmlNameSpace:@"http://tempuri.org/" webServiceName:@"getReportMassage" wsParameters:paras];
ServiceHelper* service =[[ServiceHelper alloc] init];
NSString* requestString = [service SysServiceUrl:@"http://192.168.86.230:8088/dvteboss_ws/wsservice/ReportWS" ServiceMethodName:@"getReportMassage" SoapMessage:soapMsg];
NSLog(@"%@",requestString);
}
返回的是一个网页的源代码,并没有返回我想要的XML文件,请问这是为什么啊,是我请求发送出现问题了吗?
求高手指教!!!!!!!!!!!
[解决办法]
帮你顶一下,没做过WebService..
[解决办法]
抓过包看看请求头,各部分符合不
[解决办法]
有条件你可以抓一下android发的包,和ios发的包对比一下就清楚了,
[解决办法]
一般前两个方法没有什么大问题,有问题就是后面的两个方法,望lz耐心调试,看看你的webservice可行不?
[解决办法]
怎么看的这样混乱呢,
使用CWHessianArchiver 几行代码搞定的东西啊
NSURL *url = [NSURL URLWithString:ADDRESS];
id<HessianService> ts = (id<HessianService>)[CWHessianConnection proxyWithURL:url protocol:@protocol(HessianService)];
[CWHessianArchiver setMethodName:@"getReportMassage" forSelector:@selector(getReportMassage:endStatTime:operator:password:type:)];
NSString* returnXml = [ts getReportMassage:@"" endStatTime:@"" operator:@"" password:@"" type:@""];