#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
iPhone 呼叫Web Service 例子(轉)
相關文章
- ASP 呼叫WEB SERVICE 文件Web
- c#呼叫web serviceC#Web
- web到service簡單原理例子Web
- .NET調PHP Web Service的典型例子PHPWeb
- 如何使用ABSL程式碼呼叫Web serviceWeb
- 翻譯-使用Spring呼叫SOAP Web ServiceSpringWeb
- web service實現原理與非同步呼叫Web非同步
- web service相關轉摘Web
- c# web service轉貼C#Web
- 什麼是Web Service (轉)Web
- 什麼是web service?- SOAP Web Service & Restful Web ServiceWebREST
- 為什麼需要Web Service (轉)Web
- xml web serviceXMLWeb
- Web Service 教程Web
- 透過例子學習Lua(4)--函式的呼叫(轉)函式
- RESTful Web Service(續)RESTWeb
- Web Service 基礎Web
- Web Service入門Web
- Android中Service的一個Demo例子Android
- .NET非同步方法呼叫的例子非同步
- 透過例子學習Lua(7)--呼叫C/C++函式(轉)C++函式
- Web Service學習筆記之----JAX-RPC (轉)Web筆記RPC
- axis客戶端呼叫伺服器端方法,返回物件陣列(web service求助)客戶端伺服器物件陣列Web
- 教你入門Web ServiceWeb
- Spring Web Service教程SpringWeb
- Web Service 的工作原理Web
- Web service是什麼?Web
- 研究Web Service同仁們,不知道對如何提高呼叫Web服務有什麼好的措施?Web
- Android中的Web ServiceAndroidWeb
- Java:Web Service初入門JavaWeb
- Web Service 之 Python -- spyneWebPython
- Loadrunner11 --web serviceWeb
- srvctl add service 增加資料庫服務例子資料庫
- Windows 下 c++ 呼叫 Rust 庫的例子WindowsC++Rust
- 用Web API Client 呼叫 Web APIWebAPIclient
- WCF、Web API、WCF REST、Web Service之區別WebAPIREST
- 什麼是 Web Service--面試Web面試
- web service 的事務問題Web