A recent project to use hotel inquiries and air ticket reservation, I this pass toss, fortunately Emperor not bear, finally finally one by one fix. The choice of Ctrip, why? At present there are several large-scale domestic, such as: Where to go, Yi Long Travel, and Ctrip. Learned a lap, and finally found that Ctrip's API is free. So I chose Ctrip. If you have any doubts about the Ctrip API, or unclear, do not know how to use, please leave a message, I will reply to you as soon as possible. The information on the Internet (iOS) is relatively small, so I wrote this blog to let everyone try to take less detours.
1> first Use the Ctrip API when you need to understand what is soap?
SOAP (simpleobjectaccessprotocal, Simple Object Access Protocol) technology helps to achieve interoperability between a large number of heterogeneous programs and platforms, so that existing applications can be accessed by a wide range of users. Soap is a combination of mature HTTP-based web technology with the flexibility and extensibility of XML.
2> as an iOS developer, when we ask for an XML format, the request body is more like a sore egg. Today, let's take a look at soap, simple Object Access protocol, or simple exchange protocol. My understanding is that we send a request at the request, which takes the XML format request body. Then, after receiving our XML request body, the server returns us an XML result. Then we begin to parse. This is the process of a request.
2.1> so some students may ask, how to encapsulate the XML request body?
Here's the XML request body.
NSString *soapbody = @ "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" "<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>" "<request xmlns=\" http://ctrip.com/\ " > " <requestxml>allianceid=\" your affiliate id\ "sid=\" Your site id\ "timestamp=\" 1416436597\ "signature=\" Your signature \ " Requesttype=\ "ota_ping\" asyncrequest=\ "false\" timeout=\ "0\" messagepriority=\ "3\" </requestXML> " " </Request> " </soap:Body>" "</soap:Envelope>";
3> how to post to the server?
What we usually call the XML request body is a data,nsdata, first we need to convert the string into data, and then post to the server. This way the server responds to us when the request is received. For example:
Nsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:[nsurl Urlwithstring:webservicesurl]; NSString *msglength = [NSString stringwithformat:@ "%lu", (unsigned long) [message length]]; Add the details of the request, corresponding to the fields in the first half of the request message [request addvalue:@ text/xml; Charset=utf-8 "forhttpheaderfield:@" Content-type "]; [Request AddValue: @ "http://ctrip.com/Request" forhttpheaderfield:@ "SOAPAction"]; [Request Addvalue:msglength forhttpheaderfield:@ "Content-length"]; [Request sethttpmethod:@ "POST"]; [Request Sethttpbody:[message datausingencoding:nsutf8stringencoding]; Nsurlconnection *connection=[[nsurlconnection alloc] initwithrequest:request delegate:self]; if (connection) { Soapdata=[[nsmutabledata alloc] init]; } [Connection start];
4> good, to now basically has completed the request of the steps, the rest is to parse. We can be in- (void) Connection: (nsurlconnection*) connection didreceivedata: (NSData*) The data method prints data, and if there is a value it indicates that our request was successful.
Analytic words we go online search kissxml, can learn from, write good.
Integrated Ctrip API under iOS app (hotel and airline tickets)