iOS calls WebService already encapsulates the SOAP variable, just call it right.
Full project: Click to download
soapconn.m//webservciebysoap////Created by fengs on 14-11-17.//Copyright (c) 2014 fengs. All rights reserved.//#import "SoapConn.h" #import "GDataXMLNode.h" #import "ASIHTTPRequest.h" #import "nsstring+ XMLExtensions.h "static soapconn *sharedinstance = nil, @implementation soapconn@synthesize printlog = _printlog; #define Ws_timeout 20.0-(id) init{self = [super init]; if (self) {_printlog = NO; } return self;} -(void) dealloc{if (sharedinstance) {[sharedinstance release]; Sharedinstance = nil; } [Super Dealloc];} + (Soapconn *) sharedinstance {if (sharedinstance = = nil) {sharedinstance = [[Super alloc] init]; } return sharedinstance;} + (void) Finish {if (sharedinstance) {[sharedinstance release]; Sharedinstance = nil; }}+ (Nsarray *) Sortarray: (Nsarray *) DataArray Withkey: (NSString *) key ascending: (BOOL) Ascending {if (!dataarray | |!key ) {return nil;} Nssortdescriptor *sortdescriptor = [[[NSSOrtdescriptor alloc] Initwithkey:key ascending:ascending] autorelease]; Nsmutablearray *array = [[[Nsmutablearray alloc] initwitharray:dataarray] autorelease]; return [array Sortedarrayusingdescriptors:[nsarray arraywithobject:sortdescriptor];} /** * WebService Call * @param soapurl:webservice address * @param namespace:webservice NameSpace * @param MethodName: WebService Method Name * @param parameter:webservice The specific parameters of the method * @return NSString */-(nsstring*) Wscorewithurl: (nsurl*) Soapurl NameSpace: (nsstring*) soapnamespace MethodName: (NSString *) soapmethodname Parameter: (soapconnparameter*) soapparameter{if (!soapmethodname) {nsexception *ex = [nsexception exceptionwithname:@ "Error" reason:@ "Functio n wscorewithmethodname Parameter invaild! " Userinfo:nil]; @throw ex; } NSString *soapaction; if ([Soapnamespace hassuffix:@ "/"]) {soapaction = [nsstring stringwithformat:@ "%@%@", Soapnamespace,soapmethodname] ; } else {soapaction = [nsstring stringwithformat:@ "%@/%@", Soapnamespace,soapmethodname]; } nsstring *soapmessage = @ "<?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" "<! $methodName $! Xmlns=\ "! $nameSpace $!\" >\n ""! $soapBody $! " "</! $methodName $!>\n" "</soap:body>\n" "</soap:Envelope>"; NSString *soapparambody = @ ""; if (soapparameter) {Nsarray *keys = [Soapparameter Getkeys]; for (int i = 0; i< keys.count; i++) {NSString *paramkey = [keys objectatindex:i]; NSObject *obj = [Soapparameter getvalueatkeyindex:i]; if ([obj Iskindofclass:[nsnumber class]]) {soapparambody = [soapparambody stringbyappendingformat:@ "<%@ >%@</%@>\n ", Paramkey, ((nsnumber*) obj). Stringvalue,paramkey]; } else if ([obj Iskindofclass:[nsdata class]]) {nsstring *encodingbytes = [asihttprequest base64fordata: (N sdata*) obj]; Soapparambody = [Soapparambody stringbyappendingformat:@ "<%@>%@</%@>\n", param Key,encodingbytes,paramkey]; } else if ([obj iskindofclass:[nsstring class]]) {soapparambody = [soapparambody stringbyappendingformat:@ "<%@>%@</%@>\n", paramkey,[nsstring Encodexmlcharactersin: (nsstring*) Obj],para Mkey]; } else {soapparambody = [soapparambody stringbyappendingformat:@ "<%@>%@</%@>\n", Paramkey,obj,paramkey]; }}} SoapMessage = [SoapMessage stringbyreplacingoccurrencesofstring:@ "! $nameSpace $!" Withstring:soapnamespa CE]; SoapMessage = [SoapMessage stringbyreplacingoccurrencesofstring:@ "! $methodName $!" Withstring:soapmethodname]; SoapMessage = [SoapMessage stringbyreplacingoccurrencesofstring:@ "! $soapBody $!" withstring:soapparambody]; if (_printlog) {NSLog (@ "post:%@", SoapMessage); } asihttprequest *request = [ASIHTTPRequest Requestwithurl:soapurl]; [Request addrequestheader:@ "Content-type" value:@ "text/xml; Charset=utf-8"]; [Request addrequestheader:@ "SOAPAction" value:soapaction]; [Request addrequestheader:@ "Content-length" value:[nsstring stringwithformat:@ "%d", [SoapMessage Length]]; [Request Appendpostdata:[soapmessage datausingencoding:nsutf8stringencoding]; [Request setrequestmethod:@ "POST"]; [Request Settimeoutseconds:ws_timeout]; [Request startsynchronous]; Nserror *error = [request ERROR]; if (!error && [request responsestatuscode] = =) {if (_printlog) {NSLog (@ "Response string:%@ ", [request responsestring]); NSLog (@ "Get HTTPresponse:%d encoding:%d length:%d ", [Request Responsestatuscode],[request responseencoding], [Request RESP Onsedata].length); } nsstring *content = [[[NSString Alloc] initwithdata:[request responsedata] encoding:[request responseencoding]] A Utorelease]; NSString *returncontent = [self getxmlresultwithmethodname:soapmethodname soapxmlstring:content]; return returncontent; } else {if (_printlog) {if (error) {NSLog (@ "%s error:%@", __func__, error); } else {NSLog (@ "%scode:%d encoding:%d desc:%@", __func__, [request Responsestatuscode], [Request Responseencoding],[request responsestring]); }}} return nil;} /** * WebService return * @param methodname:webservice method name * @param soapxmlstring:soap return information * @return NSString */-(NSS tring*) Getxmlresultwithmethodname: (nsstring*) methodName soapxmlstring: (nsstring*) soapxmlstring{NSError *err =Nil Gdataxmldocument *soapxml = [[Gdataxmldocument alloc] Initwithxmlstring:soapxmlstring options:0 error:&err]; if (!soapxml) {return nil; } if (err) {[Soapxml release]; NSLog (@ "%s%@", __func__, err); return nil; } nsstring *retval = @ ""; Gdataxmlelement *rootelement = [Soapxml rootelement]; Nsarray *result= [rootelement children]; while ([result count]>0) {nsstring *nodename = [[result objectatindex:0] name]; if ([NodeName isequaltostring: [nsstring stringwithformat:@ "% @Result", MethodName]]) {Gdataxmlnode *resultnode = [result objectatindex:0]; if ([ResultNode ChildCount] > 0) {nsstring *contentstr = [NSString decodexmlcharactersin:[resultnode XM Lstring]]; CONTENTSTR = [Contentstr stringbyreplacingoccurrencesofstring:[nsstring stringwithformat:@ "<%@>", NodeName] Withstring:@ "" Options:nscaseinsensitivesearch range:nsmakerange (0, nodename.length+2)]; CONTENTSTR = [Contentstr stringbyreplacingoccurrencesofstring:[nsstring stringwithformat:@ "</%@>", NodeName] withstring:@ "" Options:nscaseinsensitivesearch Range:nsmakerange (contentstr.length-nodename.length-3, NODENAME.LENGTH+3)]; RetVal = Contentstr; } break; } result = [[result objectatindex:0] children]; } [Soapxml release]; return retVal;} @end
iOS Webservice+soap programming