在 iPhone 上使用 Objective-C 使用 WCF Web 服务 [英] Consume WCF Web Service using Objective-C on iPhone

查看:23
本文介绍了在 iPhone 上使用 Objective-C 使用 WCF Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I was able to accomplish this on a .asmx web service, but not with a WCF service.

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

How do I know the correct format of the request SOAP message?

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

The web service I am trying to hit has a format of: http://xxx.xxx.xxx.xxx:PORT/IService1/ (running locally in a VM)

我很抱歉缺乏信息,我很迷茫.

I apologize for the lack of information, I am pretty lost.

非常感谢任何和所有帮助.

Any and all help is much appreciated.

推荐答案

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

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"?>
"
"<SOAP-ENV:Envelope 
"
"xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
"
"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
" 
"xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
"
"SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
"
"xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
"
"<SOAP-ENV:Body> 
"
"<Login xmlns="http://tempuri.org/"><username>JACKSON</username><password>PASSWORD</password>"
"</Login> 
"
"</SOAP-ENV:Body> 
"
"</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

这篇关于在 iPhone 上使用 Objective-C 使用 WCF Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆