Iphone访问WCF服务 (xcode代码)
1 #import "wcf2ViewController.h" 2 3 @implementation wcf2ViewController 4 @synthesize label; 5 6 -(void)viewDidLoad { 7 8 [superviewDidLoad]; 9 //Web Service Call 10 NSString *soapMessage = [NSStringstringWithFormat: 11 @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 12 "<SOAP-ENV:Envelope \n" 13 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n" 14 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" 15 "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n" 16 "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n" 17 "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n" 18 "<SOAP-ENV:Body> \n" 19 "<GetUser xmlns=\"http://tempuri.org/\">" 20 "</GetUser> \n" 21 "</SOAP-ENV:Body> \n" 22 "</SOAP-ENV:Envelope>"]; 23 24 //[[NSURLCache sharedURLCache] removeAllCachedResponses]; 25 NSURL *url = [NSURL URLWithString:@"http://192.168.1.3:6001/IPhone.I500.svc"]; 26 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 27 NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 28 29 [theRequest addValue: @"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"]; 30 [theRequest addValue: @"http://tempuri.org/IIPHONESERVERS/GetUser"forHTTPHeaderField:@"SOAPAction"]; 31 [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 32 [theRequest setHTTPMethod:@"POST"]; 33 [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 34 35 NSURLConnection *theConnection = [[NSURLConnectionalloc] initWithRequest:theRequest delegate:self]; 36 if(theConnection) { 37 webData = [[NSMutableDatadata] retain]; 38 } 39 else { 40 NSLog(@"theConnection is NULL"); 41 } 42 } 43 44 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 45 [webDatasetLength:0]; 46 } 47 48 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 49 [webData appendData:data]; 50 } 51 52 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 53 label.text = [NSStringstringWithFormat:@"Connection failed: %@", [error description]]; 54 } 55 56 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 57 [connection release]; 58 NSLog(@"Data has been loaded"); 59 NSXMLParser *parser = [[NSXMLParseralloc] initWithData:webData]; 60 [parser setDelegate:self]; 61 [parser parse]; 62 [webDatarelease]; 63 } 64 65 - (void)parserDidStartDocument:(NSXMLParser *)parser { 66 } 67 68 #pragma mark --遍历Xml每个节点 69 70 - (void)parser:(NSXMLParser *)parser 71 didStartElement:(NSString *)elementName 72 namespaceURI:(NSString *)namespaceURI 73 qualifiedName:(NSString *)qualifiedName 74 attributes:(NSDictionary *)attributeDict { 75 NSLog(@"Name:%@",elementName); 76 currentElement = elementName; 77 if ([currentElementisEqualToString:@"GetUserResult"]) 78 { 79 label.text=@""; 80 UIAlertView *view = [[UIAlertViewalloc] initWithTitle:@"调用wcf成功!" message:nildelegate:nilcancelButtonTitle:@"Ok"otherButtonTitles:nil] ; 81 [view show]; 82 [view release]; 83 } 84 } 85 86 #pragma mark --当XMl有值时,进入此句 87 88 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 89 NSString *fixedString = [string stringByTrimmingCharactersInSet: 90 [NSCharacterSetwhitespaceAndNewlineCharacterSet]]; 91 NSLog(@"Element:%@ __ Value:%@",currentElement,string); 92 } 93 94 95 #pragma mark --当遇到结束标记时,进入此句 96 97 - (void)parser:(NSXMLParser *)parser 98 didEndElement:(NSString *)elementName 99 namespaceURI:(NSString *)namespaceURI 100 qualifiedName:(NSString *)qName { 101 } 102 103 - (void)parserDidEndDocument:(NSXMLParser *)parser { 104 } 105 106 - (void)dealloc { 107 [super dealloc]; 108 } 109 @end