zoukankan      html  css  js  c++  java
  • 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"?>
    "
     12     "<SOAP-ENV:Envelope 
    "
     13     "xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    "
     14     "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    " 
     15     "xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    "
     16     "SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
    "
     17     "xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    "
     18     "<SOAP-ENV:Body> 
    "
     19     "<GetUser xmlns="http://tempuri.org/">"
     20     "</GetUser> 
    "
     21     "</SOAP-ENV:Body> 
    "
     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
  • 相关阅读:
    scala-隐式转换
    scala-泛型
    scala-模式匹配
    scala-LinkedList
    scala-高阶函数
    scala-数组/列表
    scala-map
    scala语法
    机器学习笔记
    Day 1 下午
  • 原文地址:https://www.cnblogs.com/Liaoyang/p/3351219.html
Copyright © 2011-2022 走看看