Soap-xml request (Integrated Ctrip API under iOS app)

Source: Internet
Author: User

Using Ctrip as an example: Ctrip Alliance air tickets, tickets Alliance id:278639 site id:739462 Key key:be57b925-e8ce-4aa2-ac8e-3ee4bbbb686f API_URL:openapi.ctrip.com Http://open.ctrip.com/InlandFlight/OTA_FlightSearch.aspx

1. First , when using the Ctrip API, 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 will be more painful than the 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.

3. Then some students may ask, how to encapsulate the XML request body?

NSString *soapbody = [NSString stringwithformat:@ "<?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>"

------Below is the XML-----//

"<Request>"

"

"<HotelRequest>"

"<requestbody xmlns:ns=\" http://www.opentravel.org/ota/2003/05\ "xmlns:xsi=\" http://www.w3.org/2001/ Xmlschema-instance\ "xmlns:xsd=\" Http://www.w3.org/2001/xmlschema\ ">"

"<FlightSearchRequest>"

"<SearchType>%@</SearchType>"

"<Routes>"

"<FlightRoute>"

"<DepartCity>%@</DepartCity>"

"<ArriveCity>%@</ArriveCity>"

"<DepartDate>%@</DepartDate>"

"</FlightRoute>"

"</Routes>"

"<PriceTypeOptions>"

"</PriceTypeOptions>"

"<ProductTypeOptions>Normal</ProductTypeOptions>"

"<Classgrade>%@</Classgrade>"

"</FlightSearchRequest>"

"</RequestBody>"

"</HotelRequest>"

"</Request>"

-----The above section is XML-------//

"</soap:Body>"

"</soap:Envelope>", Searchtype,departcity,arrivecity,departdate,classgrade];

4. How do I 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.

Api_url, interface address

Nsurl *url = [Nsurl urlwithstring:@ "Http://openapi.ctrip.com/Flight/DomesticFlight/OTA_FlightSearch.asmx"];

Nsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:url];

NSString *msglength = [NSString stringwithformat:@ "%lu", (unsigned long) [soapbody length]];

[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:[soapbody datausingencoding:nsutf8stringencoding];

Nsurlconnection *connection=[[nsurlconnection alloc] initwithrequest:request delegate:self];

if (connection) {

NSLog (@ "Ticket search succeeded");

} [connection start];

5. Set proxy <nsurlconnectiondelegate>, implement Proxy

/**

* Data returned after a successful request

*

* @param Connection Connection

* @param data

*/

-(void) connection: (nsurlconnection*) connection didreceivedata: (nsdata*) data{

NSLog (@ "%@", data);

nsstring* strpartdata=[[nsstring alloc] Initwithdata:data encoding:nsutf8stringencoding];

NSLog (@ "= = =%@", strpartdata);

[Self.strreceiveddata Appendstring:strpartdata];

}

6. Start parsing, #import "GDataXMLNode.h"

-(void) connectiondidfinishloading: (nsurlconnection *) connection{

NSLog (@ "%@ connectiondidfinish.\n", self);

Created from string content, Gdataxmldocument instances

Nserror*error = nil;

Gdataxmldocument *doc = [[Gdataxmldocument alloc]initwithxmlstring:self.strreceiveddata options:0 error:&error];

if (Error)

{

NSLog (@ "Create gdataxmldocument error,error Code:%@", [Error localizeddescription]);

Return

}

First find the root element

Gdataxmlelement *rootelement = [Doc rootelement];

Find all child elements with name "Student" inside the root element

}

Soap-xml request (Integrated Ctrip API under iOS app)

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.