ASIHTTPRequest访问。NET的WebService  基于soap协议

1.通过浏览器浏览WebService,会大概提供如下的信息:
ASIHTTPRequest访问。NET的WebService <wbr> <wbr>基于soap协议


2.构建soap消息体:
   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/">" 
                             "<soap:Body>\n" 
                             "<ExecuteDataset xmlns="http://tempuri.org/">" 
                             "<sql>%@</sql>"
                             "</ExecuteDataset>"
                             "</soap:Body>\n" 
                             "</soap:Envelope>",sql];//就是把上述中的那段xml填进来
   NSString * wsURL = @"http://192.168.9.12:8082/ZTService.asmx";//ZTService.asmx就相当与.NET那边一个文件

    //请求发送到的路径
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",wsURL]];
    ASIHTTPRequest * theRequest = [ASIHTTPRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
     
    //以下对请求信息添加属性前四句是必有的,第五句是soap信息。 对应1中的那些信息
    [theRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
    [theRequest addRequestHeader:@"SOAPAction" value:@“http://tempuri.org/ExecuteDataset”];
     
    [theRequest addRequestHeader:@"Content-Length" value:msgLength];
    [theRequest setRequestMethod:@"POST"];
    [theRequest appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
     
    [theRequest setDefaultResponseEncoding:NSUTF8StringEncoding];
//显示网络请求信息在status bar上
    [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:YES];
     
    //同步调用
    [theRequest startSynchronous];

3.采用kissXML开源库解析返回回来的xml字符串
NSLog(@"返回的soap信息是:%@",returnSoapXML); 
    //开始解析xml 
  
    DDXMLDocument *doc = [[DDXMLDocument alloc] initWithXMLString:returnSoapXML options:0 error:nil];
   
    /////解析
  
    NSArray *items = [doc nodesForXPath:@"//Table" error:nil];
    //NSLog(@"%@",items);
   
   
    for (DDXMLElement *obj in items) {//循环查询的每条记录
        //NSLog(@"%@",obj.children);
        for(DDXMLNode *node in obj.children){//取得每个字段的值
         NSLog(@"%@",node.stringValue);
        }
     
    }

原文地址:https://www.cnblogs.com/cnsec/p/11515879.html