在 iPhone 上使用 Objective-C 使用 WCF Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/982622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consume WCF Web Service using Objective-C on iPhone
提问by JWD
I am having a hard time consuming a very simple (Hello World) WCF web service in my iPhone app. From what I have read, you must manually create the request message then send it to the web service URL.
我很难在我的 iPhone 应用程序中使用一个非常简单的(Hello World)WCF Web 服务。根据我的阅读,您必须手动创建请求消息,然后将其发送到 Web 服务 URL。
I was able to accomplish this on a .asmx web service, but not with a WCF service.
我能够在 .asmx Web 服务上完成此操作,但无法在 WCF 服务上完成。
How do I know the correct format of the request SOAP message?
我如何知道请求 SOAP 消息的正确格式?
The web service I am trying to hit has a format of: http://xxx.xxx.xxx.xxx:PORT/IService1/(running locally in a VM)
我尝试使用的 Web 服务的格式为:http: //xxx.xxx.xxx.xxx: PORT/IService1/(在 VM 中本地运行)
I apologize for the lack of information, I am pretty lost.
我为缺乏信息道歉,我很迷茫。
Any and all help is much appreciated.
非常感谢任何和所有帮助。
回答by JWD
Thank to everyone that helped here. I ended up figuring it out and thought I would share my results. I know this is not a comprehensive solution, so shoot me a message or comment if you require more detail.
感谢所有在这里提供帮助的人。我最终弄清楚了,并认为我会分享我的结果。我知道这不是一个全面的解决方案,所以如果您需要更多细节,请给我留言或评论。
//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>HymanSON</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
回答by Lounges
Personally i would recommend adding a REST based endpoint for the WCF service. You can run it simultaneously with the SOAP and its a LOT easier to consume on the iPhone side.
我个人建议为 WCF 服务添加一个基于 REST 的端点。您可以将它与 SOAP 同时运行,它在 iPhone 端更容易使用。
回答by Mehmet Aras
What kind of error are you getting when you hit our WCF service? So asmx is working but not WCF? One thing you need to pay attention is that in .NET 2.0, the web services (asmx) supported both SOAP 1.1 and SOAP 1.2 messages. However, basicHttpBinding in WCF handles only SOAP 1.1. So if you're sending SOAP 1.2 messages from your iPhone client to the WCF service with basicHttpBinding, this could very well be the problem you're having. In order to support SOAP 1.2, you need to use wsHttpBinding or create a custom binding pretty much the same as basicHttpBinding but you specify the message version to be SOAP 1.2.
当您点击我们的 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>
回答by yasirmturk
Caged / httpriotSimple HTTP Rest Library for iPhone and Cocoa projects.
Caged / httpriot用于 iPhone 和 Cocoa 项目的简单 HTTP Rest 库。
回答by Chris Porter
Have you tried to access the WCF service as a Web Service from a windows client? I haven't configured WCF to run as an asmx styled Web Service but I believe there are some specific settings that have to be tweaked to get WCF to act like in that manner. Working with a windows client would take out any iphone based issues out of the picture for testing purposes and would help you narrow your testing to the service itself.
您是否尝试从 Windows 客户端将 WCF 服务作为 Web 服务访问?我还没有将 WCF 配置为作为 asmx 样式的 Web 服务运行,但我相信必须调整一些特定设置才能使 WCF 以这种方式运行。使用 Windows 客户端可以从图片中删除任何基于 iphone 的问题以进行测试,并帮助您将测试范围缩小到服务本身。
回答by Chris Porter
You need to make sure that you are using BasicHttpBinding (not WSHttpBinding) which is aimed more are clients that are not on the .NET 3.0 platform. This Binding does not support security, reliability or ordered delivery.
您需要确保您使用的是 BasicHttpBinding(而不是 WSHttpBinding),它的目标更多是不在 .NET 3.0 平台上的客户端。此绑定不支持安全性、可靠性或有序交付。

