& Lt; iOS & gt; network programming SOAP, WSDL, Web Service

Source: Internet
Author: User

1. Open the above URL. On the left side, we can see two methods with the same name: LocalTimeByZipCode. The following one is used to bind LocalTimeSoap12. Ignore it and open the above. The Overview description of the method is displayed, including Input Parameters and Output Parameters. click Test Form and enter a zipcode for online testing, for example, enter 12345. this test shows that this web service can currently provide services. Click "Message Layout". OK. We can see three methods of use: Soap, HTTP Get, and HTTP Post. Here we focus on the SOAP method. In the SOAP section, the above box shows the SOAP content package to be submitted when a request is initiated. The following shows the SOAP information package for normal response. This is what we want to see: POST/webservices/LocalTime. asmxSOAPAction: http://www.ripedev.com/LocalTimeByZipCodeContent-Type: text/xml; charset = utf-8Content-Length: stringHost: string <? Xml version = "1.0" encoding = "UTF-16"?> <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> <LocalTimeByZipCode xmlns = "http://www.ripedev.com/"> <ZipCode> string </ZipCode> </LocalTimeByZipCode> </soap: Body> </soap: envelope> then we will create our xcode project. Just like the previous one, we will create a button and then a textView for display. This is my button event:-(void) startRequestToWsdl2 :( id) sender {NSString * soapMessage = [NSString stringWithFormat: @ "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> \ N "" <soap: Envelope xmlns: xsi = \ "external" xmlns: xsd = \ "http://www.w3.org/2001/XMLSchema\" xmlns: soap = \ "external"> \ n "" <soap: body> \ n "" <LocalTimeByZipCode xmlns = \ "http://www.ripedev.com/\"> "" <ZipCode> 12345 </ZipCode> \ n "" </LocalTimeByZipCode> \ n "< /soap: body> \ n "" </soap: Envelope> \ n "]; // By The Way, in SOAP, the Header part is dispensable, and the Fault part is dispensable, but the Body and Envel Ope must have. // The above part is almost in the format given by SOAP. The following address is from where it comes from this Web site, that is above we want to thank this Web site: http://www.ripedevelopment.com/webservices/LocalTime.asmxNSString * address = @ "http://www.ripedevelopment.com/webservices/LocalTime.asmx"; NSURL * url = [NSURLURLWithString: address]; NSMutableURLRequest * theRequest = [NSMutableURLRequestrequestWithURL: url]; // text/xml, and content-Length must exist. [TheRequest addValue: @ "text/xml; charset = UTF-8" forHTTPHeaderField: @ "Content-Type"]; NSString * msgLength = [NSString stringWithFormat: @ "% d ", [soapMessage length]; [theRequest addValue: msgLength forHTTPHeaderField: @ "Content-Length"]; // In the following line, SOAPAction is followed, and where does the following URL come from, from the red and bold parts above. [TheRequest addValue: @ "http://www.ripedev.com/LocalTimeByZipCode" forHTTPHeaderField: @ "SOAPAction"]; [theRequestsetHTTPMethod: @ "POST"]; [theRequest setHTTPBody: [soapMessage dataUsingEncoding: encoding]; NSURLConnection * theConnection = [[NSURLConnectionalloc] initWithRequest: theRequestdelegate: self]; if (theConnection) {webData = [[NSMutableData data] retain];} else {NSLog (@ "theConnect Ion is NULL ");} Okay. The request has been initiated. Next we will receive the data and perform XMLParse parsing. -(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {[webDatasetLength: 0]; NSLog (@ "connection: didReceiveResponse: 1");}-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {[webDataappendData: data]; NSLog (@ "connection: didReceiveData: 2");} // if the computer is not connected to the network, this message appears (not because the network server is disconnected)-(void) connection :( NSURLConnection *) connection didFailWithError: (NSError *) error {NSLog (@ "ERROR with theConenction"); [connection release]; [webDatarelease];}-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "3 DONE. received Bytes: % d ", [webDatalength]); NSString * theXML = [[NSStringalloc] initWithBytes: [webDatamutableBytes] length: [webDatalength] encoding: Bytes]; NSLog (@ "received data = % @", theXML); [theXML release]; // re- Add external xmlParser if (xmlParser) {[xmlParserrelease];} xmlParser = [[NSXMLParseralloc] initWithData: webData]; [xmlParsersetDelegate: self]; [External: YES]; [xmlParser parse]; [connection release];} // The above completes data receiving. The NSXMLParser is parsed below. -(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes: (NSDictionary *) attributeDict {NSLog (@ "4 parser didStarElemen: namespaceURI: attributes: % @", elementName); if ([elementNameisEqualToString: @ "LocalTimeByZipCodeResult"]) {if (! SoapResults) {soapResults = [[NSMutableString alloc] init];} recordResults = YES ;}}-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {NSLog (@ "5 parser: foundCharacters:"); if (recordResults) {[soapResultsappendString: string] ;}- (void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {NSLo G (@ "6 parser: didEndElement:"); if ([elementnameisinclutostring: @ "LocalTimeByZipCodeResult"]) {recordResults = FALSE; NSLog (@ "incluedresult timezone = % @", soapResults); [soapResultsrelease]; soapResults = nil; NSLog (@ "hoursOffset result") ;}}-(void) parserDidStartDocument :( NSXMLParser *) parser {NSLog (@ "------------------- start --------------");}-(void) parserDidEndDocument :( NSXMLParser *) parser {NSL Og (@ "------------------- end ------------");} Let's check again, what is the data returned by this web service? HTTP/1.0 200 OKContent-Type: text/xml; charset = utf-8Content-Length: string <? Xml version = "1.0" encoding = "UTF-16"?> <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> <LocalTimeByZipCodeResponse xmlns = "http://www.ripedev.com/"> <LocalTimeByZipCodeResult> string </LocalTimeByZipCodeResult> </LocalTimeByZipCodeResponse> </soap: Body> www.2cto.com </soap: envelope> // bold and green text on the top. We can see that I Here is what we are looking. So that's why we need to use LocalTimeByZipCodeResult in Parse parsing. Okay, run it and check that the data has been returned. (PS: The above is a direct number. in the real environment, the thread method should be used to avoid affecting the main interface thread .)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.