【问题标题】:Consume WCF Web Service using Objective-C on iPhone在 iPhone 上使用 Objective-C 使用 WCF Web 服务
【发布时间】:2010-11-02 05:29:40
【问题描述】:

我很难在我的 iPhone 应用程序中使用一个非常简单的 (Hello World) WCF Web 服务。根据我的阅读,您必须手动创建请求消息,然后将其发送到 Web 服务 URL。

我能够在 .asmx Web 服务上完成此操作,但不能使用 WCF 服务。

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

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

我很抱歉缺少信息,我很迷茫。

非常感谢任何和所有的帮助。

【问题讨论】:

  • JWD,我很好奇你能用 wcftestclient 检测到服务吗?这是远程托管服务还是您在本地运行?
  • 我可以使用 wcftestclient 查看和调用它,它在我的 macbook 上运行,在 windows XP VMWare 映像内,在 Visual Studio 内。

标签: iphone objective-c wcf web-services


【解决方案1】:

感谢所有在这里提供帮助的人。我最终弄清楚了,并认为我会分享我的结果。我知道这不是一个全面的解决方案,所以如果您需要更多详细信息,请给我留言或评论。

//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

【讨论】:

  • 嗨。我正在尝试以类似的方式访问 svc 文件,但没有得到响应 - stackoverflow.com/questions/1327673/…。真的可以使用一些帮助!谢谢。
  • 嗨,我正在尝试使用一个 svc web 方法,该方法期望一个具有图像和一些字符串作为参数的对象....我有点困惑为相同的创建 post 方法....有什么建议吗??
  • @lostInTransit 我也在尝试访问网络服务,但未能获得所需的结果。我可以从你那里得到任何帮助吗?
  • @devaditya 嘿兄弟,我正在尝试和你的一样。你找到解决办法了吗?
  • @hpiOSCoder 如果您有特定问题,您可能想发布一个单独的问题(并且答案没有帮助)
【解决方案2】:

我个人建议为 WCF 服务添加一个基于 REST 的端点。您可以与 SOAP 同时运行它,而且它在 iPhone 端更容易使用。

http://msdn.microsoft.com/en-us/library/dd203052.aspx

【讨论】:

    【解决方案3】:

    您在访问我们的 WCF 服务时遇到了什么样的错误?所以 asmx 工作但 WCF 不工作?您需要注意的一件事是,在 .NET 2.0 中,Web 服务 (asmx) 同时支持 SOAP 1.1 和 SOAP 1.2 消息。但是,WCF 中的 basicHttpBinding 仅处理 SOAP 1.1。因此,如果您使用 basicHttpBinding 从 iPhone 客户端向 WCF 服务发送 SOAP 1.2 消息,这很可能是您遇到的问题。为了支持 SOAP 1.2,您需要使用 wsHttpBinding 或创建与 basicHttpBinding 几乎相同的自定义绑定,但您将消息版本指定为 SOAP 1.2。

    <customBinding>
      <binding name="customHttpBindingWithSoap12">
         <textMessageEncoding messageVersion="Soap12"/>
         <httpTransport />              
      </binding>
    </customBinding>   
    

    【讨论】:

      【解决方案4】:

      Caged / httpriot iPhone 和 Cocoa 项目的简单 HTTP Rest 库。

      【讨论】:

        【解决方案5】:

        您是否尝试过将 WCF 服务作为 Web 服务从 Windows 客户端访问?我还没有将 WCF 配置为作为 asmx 样式的 Web 服务运行,但我相信必须调整一些特定设置才能让 WCF 以这种方式运行。使用 Windows 客户端可以消除任何基于 iphone 的问题以进行测试,并帮助您将测试范围缩小到服务本身。

        【讨论】:

        • 我没有尝试过 Windows 客户端,只是从 Visual Studio 运行的 WCF 测试客户端。我相信该服务运行正常,我的问题更侧重于在 iPhone 上使用目标 C 来使用它。
        【解决方案6】:

        您需要确保您使用的是 BasicHttpBinding(不是 WSHttpBinding),它更多地针对不在 .NET 3.0 平台上的客户端。此绑定不支持安全性、可靠性或有序交付。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多