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

在iPhone上使用Objective-C使用WCF Web服务,iphone,objective-c,wcf,web-services,Iphone,Objective C,Wcf,Web Services,我很难在iPhone应用程序中使用一个非常简单(Hello World)的WCF web服务。根据我所读到的,您必须手动创建请求消息,然后将其发送到web服务URL 我能够在.asmxweb服务上实现这一点,但不能在WCF服务上实现 如何知道请求SOAP消息的正确格式 我尝试访问的web服务的格式为: (在VM中本地运行) 我很抱歉没有提供信息,我很迷路 非常感谢您的帮助。您是否尝试过从windows客户端以Web服务的形式访问WCF服务?我还没有将WCF配置为作为asmx样式的Web服务运行,

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

我能够在.asmxweb服务上实现这一点,但不能在WCF服务上实现

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

我尝试访问的web服务的格式为: (在VM中本地运行)

我很抱歉没有提供信息,我很迷路


非常感谢您的帮助。

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

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

当您点击我们的WCF服务时,您会遇到什么样的错误?那么,asmx正在工作,但WCF没有?需要注意的一点是,在.NET2.0中,web服务(asmx)同时支持SOAP1.1和SOAP1.2消息。但是,WCF中的basicHttpBinding只处理SOAP 1.1。因此,如果您使用basicHttpBinding将SOAP 1.2消息从iPhone客户端发送到WCF服务,这很可能就是您遇到的问题。为了支持SOAP1.2,您需要使用wsHttpBinding或创建一个与basicHttpBinding几乎相同的自定义绑定,但需要将消息版本指定为SOAP1.2

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

感谢所有在这里帮助过的人。我最终找到了答案,并想与大家分享我的结果。我知道这不是一个全面的解决方案,所以如果您需要更多细节,请给我留言或评论

//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
//使用的变量
NSMutableData*webData;
NSMutableString*soapResults;
NSXMLParser*xmlParser;
BOOL记录结果;
//Web服务调用
NSString*soapMessage=[NSString stringWithFormat:
@“\n”
“\n”
“\n”
“JACKSONPASSWORD”
“\n”
“\n”
""];
NSURL*url=[NSURL URLWithString:@”http://172.16.0.142:8731/Service1/"];               
NSMutableURLRequest*theRequest=[NSMutableUrlRequestWithURL:url];
NSString*msgLength=[NSString stringWithFormat:@“%d”,[soapMessage length]];
[theRequest addValue:@“text/xml;charset=utf-8”用于HttpHeaderField:@“Content Type”];
[请求附加值:@”http://tempuri.org/IService1/LoginforHTTPHeaderField:@“Soapaction”];
[theRequest addValue:msgLength for HttpHeaderField:@“内容长度”];
[TheRequestSetHttpMethod:@“POST”];
[TheRequestSetHttpBody:[soapMessage数据使用编码:NSUTF8StringEncoding];
NSURLConnection*连接=[[NSURLConnection alloc]initWithRequest:theRequest委托:self];
if(连接){
webData=[[NSMutableData]保留];
}
否则{
NSLog(@“连接为空”);
}
//实现NSURL和XMLParser协议
#布拉格标记-
#pragma-mark-nsurl连接方法
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
#布拉格标记-
#pragma标记XMLParser方法
-(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName
namespaceURI:(NSString*)namespaceURI
限定名称:(NSString*)qName
属性:(NSDictionary*)属性指令
-(void)解析器:(NSXMLParser*)解析器查找字符:(NSString*)字符串
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName
namespaceURI:(NSString*)namespaceURI
限定名称:(NSString*)qName

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


用于iPhone和Cocoa项目的简单HTTP Rest库。

我没有尝试windows客户端,只是从Visual Studio运行的WCF测试客户端。我相信这项服务运行正常,我的问题更集中于在iPhone.Hi上使用objective C。我试图以类似的方式访问svc文件,但没有得到响应-。真的需要一些帮助!谢谢。您好,我正在尝试使用一个svc web方法,该方法期望一个对象具有图像和一些字符串作为参数……我有点困惑于为相同的对象创建post方法……有任何建议吗???@lostInTransit我也在尝试访问web服务,访问结果失败。。你能帮我什么忙吗?@devaditya嘿,兄弟,我也在试着和你一样的方法。您找到解决方案了吗?@hpiosconder如果您有特定问题(答案没有帮助),您可能想单独提出一个问题。JWD,我很好奇,您能使用wcftestclient检测服务吗?这是一个远程托管服务还是您在本地运行?我可以使用wcftestclient查看和调用它,它在我的macbook上运行,在windows XP VMWare映像中,在visual studio中。