详解跨平台iPhone中调用WCF服务(soap通信)
2012-01-08 03:00
357 查看
iPhone中调用WCF服务是本文要介绍的内容,由于对移动平台充满着好奇与兴趣,最近着手了iPhone开发和学习。学习的路线是从objective-c到cococa。方法是看了两本入门的英文书,还有就是学习apple的sdk。对于产品的基本想法是服务端用.net,手机客户端用iPhone。
一些复杂的逻辑处理放到服务端实现,客户端与服务端通过XML交互,在iPhone客户端解析XML通过cocoa展示数据。由于iPhone和DoNet是两个完全不同的平台。iPhone依靠mac系统平台,donet依赖windows系统平台。这篇文章我将通过一个helloworld程序讲述一下通过WCF实现从mac系统到windows的跨平台的调用。
1、创建简单的WCF服务
1、创建简单的WCF服务
服务契约代码如下
2、在iPhone中调用WCF
与donet调用wcf服务不同,这里使用NSURLConnection去获取WCF服务端的数据,代码如下:(单击可放大)
NSString *soapMessage=[NSStringstringWithFormat:
@"<?xmlversion="1.0" encoding"UTF-8"?>\n"
"<SOAP-ENV:Envelope \n"
"xmlns:xsd="http://www.w3.org/2001/XMLSchema"\n"
"xmlns:SOAP-ENC="http://schemas.xmlsoap.org/envelope/ "\n"
"SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/encoding/"\n"
"xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">\n"
"<SOAP-ENV:Body>\n"
"<GetDataxmls="http://tempuri.org/">"
"</GetData>\n"
"</SOAP-ENV:Body>\n"
"</SOAP-ENV:Envelope>"
];
NSURL *url=[NSURLURLWithString:@"http://10.5.23.117:8008/servicel.svc"];
NSMutableURLRequest*theRequest=[NSMutableURLRequestrequestWithURL:url];
NSString *msgLength=[NSStringstringWithFormat:@"%d",[soapMessage length]];
[theRequest addValue:@"text/xml;charset=utf-8"forHTTPHeaderField:@"Content-Type"];
[theRequestaddValue:@"http://tempuri.org/ISerVice1/GetData"forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLengthforHTTPHeaderField:@"Content-Length"];
[theRequestsetHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessagedataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection*theConnection=[[NSURLConnection alloc]initWithRequest:urldelegate:self];
if (theConnection) {
webData=[[NSMutableDataalloc]retain];
}
else {
NSLog(@"the Connection isnil");
}
NSURLConnection的委托方法:
-(void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse *)response
{
[receivedData setlength:0];
}
- (void)connection:(NSURLConnection *)connectiondidReceiveData:(NSData *)data
{
[receivedData appendData:data];
return;
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSString *theXML = [[NSString alloc]initWithBytes: [webData mutableBytes] length:[webData length]encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXMLrelease];
//重新加載xmlParser
if(xmlParser )
{
[xmlParserrelease];
}
xmlParser= [[NSXMLParser alloc] initWithData: webData];
[xmlParsersetDelegate: self];
[xmlParsersetShouldResolveExternalEntities: YES];
[xmlParserparse];
[connectionrelease];
//[webDatarelease];
一些复杂的逻辑处理放到服务端实现,客户端与服务端通过XML交互,在iPhone客户端解析XML通过cocoa展示数据。由于iPhone和DoNet是两个完全不同的平台。iPhone依靠mac系统平台,donet依赖windows系统平台。这篇文章我将通过一个helloworld程序讲述一下通过WCF实现从mac系统到windows的跨平台的调用。
1、创建简单的WCF服务
1、创建简单的WCF服务
服务契约代码如下
2、在iPhone中调用WCF
与donet调用wcf服务不同,这里使用NSURLConnection去获取WCF服务端的数据,代码如下:(单击可放大)
NSString *soapMessage=[NSStringstringWithFormat:
@"<?xmlversion="1.0" encoding"UTF-8"?>\n"
"<SOAP-ENV:Envelope \n"
"xmlns:xsd="http://www.w3.org/2001/XMLSchema"\n"
"xmlns:SOAP-ENC="http://schemas.xmlsoap.org/envelope/ "\n"
"SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/encoding/"\n"
"xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">\n"
"<SOAP-ENV:Body>\n"
"<GetDataxmls="http://tempuri.org/">"
"</GetData>\n"
"</SOAP-ENV:Body>\n"
"</SOAP-ENV:Envelope>"
];
NSURL *url=[NSURLURLWithString:@"http://10.5.23.117:8008/servicel.svc"];
NSMutableURLRequest*theRequest=[NSMutableURLRequestrequestWithURL:url];
NSString *msgLength=[NSStringstringWithFormat:@"%d",[soapMessage length]];
[theRequest addValue:@"text/xml;charset=utf-8"forHTTPHeaderField:@"Content-Type"];
[theRequestaddValue:@"http://tempuri.org/ISerVice1/GetData"forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLengthforHTTPHeaderField:@"Content-Length"];
[theRequestsetHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessagedataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection*theConnection=[[NSURLConnection alloc]initWithRequest:urldelegate:self];
if (theConnection) {
webData=[[NSMutableDataalloc]retain];
}
else {
NSLog(@"the Connection isnil");
}
NSURLConnection的委托方法:
-(void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse *)response
{
[receivedData setlength:0];
}
- (void)connection:(NSURLConnection *)connectiondidReceiveData:(NSData *)data
{
[receivedData appendData:data];
return;
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSString *theXML = [[NSString alloc]initWithBytes: [webData mutableBytes] length:[webData length]encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXMLrelease];
//重新加載xmlParser
if(xmlParser )
{
[xmlParserrelease];
}
xmlParser= [[NSXMLParser alloc] initWithData: webData];
[xmlParsersetDelegate: self];
[xmlParsersetShouldResolveExternalEntities: YES];
[xmlParserparse];
[connectionrelease];
//[webDatarelease];
相关文章推荐
- 使用WCF进行跨平台开发之二(IIS托管WCF服务并使用php平台调用)
- sliverlight调用wcf服务详解
- iPhone开发 调用wcf服务
- 跨平台iPhone中调用WCF服务
- 跨平台iPhone中调用WCF服务
- 使用WCF进行跨平台开发之三(JAVA调用WCF服务)
- iPhone调用WCF服务
- 详解JAVA调用WCF服务的示例代码
- iPhone中调用WCF服务
- iPhone中调用WCF服务
- iPhone中调用WCF服务“.NET研究”
- WCF 配置终结点并调用服务
- Post方式调用wcf服务
- COM(VB/VBA/Script)利用服务标记调用WCF服务之四:使用配置文件
- silverlight学习<三> 记录今天silverlight调用WCF服务出现的各种状况
- 【菜鸟学WCF】使用ScriptManager+Ajax调用WCF服务之如何为服务创建接口
- JQuery 跨域调用 WCF 服务
- 动态调用WCF不添加服务(svcutil.exe)
- C#代码直接调用WCF服务
- Java与WCF交互(一):Java客户端调用WCF服务