标签:
用携程机票为例: 携程联盟 飞机票、门票 联盟ID:278639 站点ID:739462 密钥KEY:BE57B925-E8CE-4AA2-AC8E-3EE4BBBB686F API_URL:openapi.ctrip.com http://open.ctrip.com/InlandFlight/OTA_FlightSearch.aspx
1.首先在使用携程api的时候你需要明白什么是soap?
SOAP(SimpleObjectaccessPRotocal,简单对象访问协议)技术有助于实现大量异构程序和平台之间的互操作性,从而使存在的应用能够被广泛的用户所访问。SOAP是把成熟的基于HTTP的WEB技术与xml的灵活性和可扩展性组合在了一起。
2.作为ios开发者,当我们请求时需要xml格式的请求体就比较蛋疼了。今天带大家先简单看看soap,简单对象访问协议,或者叫做简单交换协议。我的理解就是我们在请求的时候发一个请求,这个请求带上xml格式请求体。然后呢,服务器在接受到我们xml请求体后,返回给我们一个xml结果。然后我们开始解析。就是这样的一个请求的过程。
3.那么有些同学可能会问了,怎么封装xml请求体呢?
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>"
//------下面是xml-----//
"<Request>"
"<Header AllianceID=\"278639\" SID=\"739462\" TimeStamp=\"1450326295\" Signature=\"4639A23E98A9141EFFC639F483828C3C\" RequestType=\"OTA_FlightSearch\" AsyncRequest=\"false\" Timeout=\"0\" MessagePriority=\"3\"/>"
"<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>"
//-----以上部分是xml-------//
"</soap:Body>"
"</soap:Envelope>",SearchType,DepartCity,ArriveCity,DepartDate,Classgrade];
4.那怎样post给服务器呢?
我们平常所说的xml请求体就是一个data,NSData,首先我们需要将字符串转换成为data,然后post给服务器。这样服务器在收到请求后就会回应我们。
//API_URL,接口地址
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(@"机票查询成功了");
} [connection start];
//5.设置代理<NSURLConnectionDelegate>,实现代理
/**
* 请求成功后返回的data
*
* @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.开始解析,#import "GDataXMLNode.h"
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"%@ connectionDidFinish.\n",self);
//通过字符串内容创建,GDataXMLDocument实例
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;
}
//首先查找到根元素
GDataXMLElement *rootElement = [doc rootElement];
//在根元素里面查找所有NAME为“Student"的子元素
}
标签:
原文地址:http://www.cnblogs.com/TheYouth/p/5055042.html