在iPhone上使用Objective-C调用WCF Web服务

37

我在我的iPhone应用中使用一个非常简单的(Hello World)WCF Web服务时遇到了困难。据我所了解,您必须手动创建请求消息,然后将其发送到Web服务URL。

我能够在.asmx Web服务上完成此操作,但是对于WCF服务却无法实现。

我如何知道请求SOAP消息的正确格式?

我试图访问的Web服务具有以下格式: http://xxx.xxx.xxx.xxx:PORT/IService1/ (在虚拟机中本地运行)

很抱歉信息不足,我感到相当迷茫。

非常感谢所有帮助。


JWD,我很好奇你能否使用wcftestclient检测到该服务?这是远程托管的服务还是在本地运行? - jr3
我可以使用wcftestclient查看和调用它,它正在我的MacBook上运行,位于Windows XP VMWare映像内部,Visual Studio也在其中。 - JWD
6个回答

46

感谢所有在这里给予帮助的人。我最终自己解决了问题,并想与大家分享我的结果。我知道这不是一个全面的解决方案,所以如果需要更多细节,请向我发送消息或评论。

//Variables used
NSMutableData *webData;
NSMutableString *soapResults;
NSXMLParser *xmlParser;
BOOL recordResults;

//Web Service Call
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<SOAP-ENV:Envelope \n"
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" 
"xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
"<SOAP-ENV:Body> \n"
"<Login xmlns=\"http://tempuri.org/\"><username>JACKSON</username><password>PASSWORD</password>"
"</Login> \n"
"</SOAP-ENV:Body> \n"
"</SOAP-ENV:Envelope>"];

NSURL *url = [NSURL URLWithString:@"http://172.16.0.142:8731/Service1/"];               
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];             
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];          
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];       
[theRequest addValue: @"http://tempuri.org/IService1/Login" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];     
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if(theConnection) {
    webData = [[NSMutableData data] retain];
}
else {
    NSLog(@"theConnection is NULL");
}

//Implement the NSURL and XMLParser protocols
#pragma mark -
#pragma mark NSURLConnection methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

#pragma mark -
#pragma mark XMLParser methods

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName
   attributes:(NSDictionary *)attributeDict 

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName

1
嗨。我尝试以类似的方式访问svc文件,但没有收到响应 - http://stackoverflow.com/questions/1327673/consuming-svc-service-from-iphone-app 。真的需要一些帮助!谢谢。 - lostInTransit
1
嗨, 我正在尝试调用一个期望参数为图像和一些字符串的对象的svc Web方法...我有点困惑如何为其创建POST方法...有什么建议吗? - devaditya
@lostInTransit 我也在尝试访问网络服务,但没有获得预期结果。我能从您那里得到任何帮助吗? - hp iOS Coder
我们需要在 WCF 服务中做出任何更改才能从 iOS 应用程序访问它吗? - Radhi
如何传递凭据以进行Sharepoint站点的身份验证。 - Nikh1414
显示剩余4条评论

10

2
当您访问我们的WCF服务时,您遇到了什么样的错误?所以asmx可以工作,但WCF不能?需要注意的一件事是,在.NET 2.0中,Web服务(asmx)支持SOAP 1.1和SOAP 1.2消息。然而,WCF中的basicHttpBinding仅处理SOAP 1.1。因此,如果您从iPhone客户端向使用basicHttpBinding的WCF服务发送SOAP 1.2消息,则可能会出现问题。为了支持SOAP 1.2,您需要使用wsHttpBinding或创建一个自定义绑定,与basicHttpBinding基本相同,但您指定消息版本为SOAP 1.2。
<customBinding>
  <binding name="customHttpBindingWithSoap12">
     <textMessageEncoding messageVersion="Soap12"/>
     <httpTransport />              
  </binding>
</customBinding>   

1

0
你尝试过从Windows客户端以Web服务的形式访问WCF服务吗?我没有配置WCF以asmx样式的Web服务运行,但我相信有一些特定的设置必须进行调整才能使WCF像那样运行。与Windows客户端一起工作将消除任何基于iPhone的问题,以便进行测试,并帮助您将测试范围缩小到服务本身。

1
我还没有尝试过Windows客户端,只是使用了从Visual Studio运行的WCF测试客户端。我相信服务已经正确运行,我的问题更集中在如何在iPhone上使用Objective C来消费它。 - JWD

0

您需要确保使用的是BasicHttpBinding(而不是WSHttpBinding),它更适用于不在.NET 3.0平台上的客户端。此绑定不支持安全性、可靠性或有序传递。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接