iPhone 调用Web Service 例子

转载 2010年11月24日 15:13:00

#import <UIKit/UIKit.h>

@interface Hello_SOAPViewController : UIViewController
{
    IBOutlet UITextField
*nameInput;
    IBOutlet UILabel
*greeting;
    NSMutableData
*webData;
    NSMutableString
*soapResults;
    NSXMLParser
*xmlParser;
    BOOL
*recordResults;
}

@property(nonatomic, retain) IBOutlet UITextField
*nameInput;
@property(nonatomic, retain) IBOutlet UILabel
*greeting;
@property(nonatomic, retain) NSMutableData
*webData;
@property(nonatomic, retain) NSMutableString
*soapResults;
@property(nonatomic, retain) NSXMLParser
*xmlParser;

-(IBAction)buttonClick: (id) sender;

@end

#import
"Hello_SOAPViewController.h"

@implementation Hello_SOAPViewController

@synthesize greeting, nameInput, webData, soapResults, xmlParser;

-(IBAction)buttonClick:(id)sender
{
    recordResults
= FALSE;
   
    NSString
*soapMessage = [NSString stringWithFormat:
   
@"<?xml version=/"1.0/" encoding=/"utf-8/"?>/n"
   
"<soap:Envelope xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//">/n"
   
"<soap:Body>/n"
   
"<Hello xmlns=/"http://viium.com//">/n"
   
"<name>%@</name>/n"
   
"</Hello>/n"
   
"</soap:Body>/n"
   
"</soap:Envelope>/n", nameInput.text
    ];
    NSLog(soapMessage);
   
    NSURL
*url = [NSURL URLWithString:@"http://viium.com/WebService/HelloWorld.asmx"];
    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://viium.com/Hello" 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");
    }
   
    [nameInput resignFirstResponder];
   
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength:
0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(
@"ERROR with theConenction");
    [connection release];
    [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(
@"DONE. Received Bytes: %d", [webData length]);
    NSString
*theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(theXML);
    [theXML release];
   
   
if( xmlParser )
    {
        [xmlParser release];
    }
   
    xmlParser
= [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];
   
    [connection release];
    [webData release];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary
*)attributeDict
{
   
if( [elementName isEqualToString:@"HelloResult"])
    {
       
if(!soapResults)
        {
            soapResults
= [[NSMutableString alloc] init];
        }
        recordResults
= TRUE;
    }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
   
if( recordResults )
    {
        [soapResults appendString:
string];
    }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   
if( [elementName isEqualToString:@"HelloResult"])
    {
        recordResults
= FALSE;
        greeting.text
= soapResults;
        [soapResults release];
        soapResults
= nil;
    }
}


/*
Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
*/

/*
Implement viewDidLoad if you need to do additional setup after loading the view.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   
// Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
// Releases the view if it doesn't have a superview
   
// Release anything that's not essential, such as cached data
}


- (void)dealloc
{
    [xmlParser release];
    [super dealloc];
}
@end

C#调用Web Service简单实例

C#调用Web Service简单实例 本文
  • u012025054
  • u012025054
  • 2014年08月31日 21:18
  • 2773

java调用.net服务例子

import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client...
  • wcd528521
  • wcd528521
  • 2015年01月04日 17:27
  • 52

WebService的基本介绍和调用实例

本文由Markdown语法编辑器编辑完成。1. WebService的基本介绍传统上,我们把计算机后台程序(Daemon)提供的功能,称为”服务”(service)。比如,让一个杀毒软件在后台运行,它...
  • inter_peng
  • inter_peng
  • 2017年09月01日 20:51
  • 729

asp 调用web service 例子

Set http=Server.CreateObject("Microsoft.XMLHTTP")http.Open "POST","http://localhost/lv/webservice/Lv...
  • mzg008
  • mzg008
  • 2006年05月31日 17:22
  • 794

webservice远程调用方法

webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使...
  • d_good
  • d_good
  • 2016年09月05日 15:58
  • 1976

c++Web Service的简单例子

程序示例(非托管)   由于已经有文章详细讲述了 VC托管程序中调用 WebService 的过程,因此以下示例只描述非托管VC程中调用WebService的方法。1、新建一个非托管的MFC应用程序,...
  • lhsxsh
  • lhsxsh
  • 2008年09月28日 11:24
  • 3601

Web Service简单入门例子

分享学习Web Service
  • caolipeng_918
  • caolipeng_918
  • 2014年09月01日 08:19
  • 1145

WSDL WebService的创建和使用实例

一. WSDL WebService的创建: 1.创建【Web Service Project】: WebServices Framework要选JAX-WS: 2.写一个简单的测试用例: p...
  • sunroyi666
  • sunroyi666
  • 2016年07月15日 15:11
  • 11855

用C++开发与调用WebService的例子

一 服务器端 1.首先编写 add.h文件: 1//gsoap ns service name: add 2//gsoap ns service namespace: http://localh...
  • kyfvc
  • kyfvc
  • 2013年08月28日 13:44
  • 1876

直接AXIS调用远程的web service

import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client...
  • yiluoAK_47
  • yiluoAK_47
  • 2015年04月08日 10:22
  • 1593
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iPhone 调用Web Service 例子
举报原因:
原因补充:

(最多只允许输入30个字)