iOS实现xml的post传递,返回xml数据进行解析
来源:互联网 发布:怎么查飞卢的订阅数据 编辑:程序博客网 时间:2024/05/21 08:04
1、xml的post传递,传参数进行拼接
-(void) postxml:(NSString*)vendor version:(NSString*)version{ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:URLstr]]; [request setHTTPMethod:@"POST"];//声明请求为POST请求 //set headers NSString *contentType = [NSString stringWithFormat:@"text/xml"];//Content-Type数据类型设置xml类型 [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; //create the body NSMutableData *postBody = [NSMutableData data]; [postBody appendData:[[NSString stringWithFormat:@"<此处写根节点><此处写报文头节点><reqUserId>123456</reqUserId><authId>LLLL</authId><authPw>123456</authPw></此处写报文头节点>"] dataUsingEncoding:NSUTF8StringEncoding]]; //拼接参数字符串 [postBody appendData:[[NSString stringWithFormat:@"<此处写报文体节点><deviceCode>"] dataUsingEncoding:NSUTF8StringEncoding]]; //vendor拼接在<deviceCode></deviceCode>标签对中间 [postBody appendData:[vendor dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"</deviceCode><appId>FaceQualityCheck</appId><operateSystem>iOS</operateSystem><operateSystemVersion>"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[version dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"</operateSystemVersion></此处写报文体节点></此处写根节点>"] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postBody]; NSString *bodyStr = [[NSString alloc] initWithData:postBody encoding:NSUTF8StringEncoding]; NSLog(@"bodyStr: %@ ",bodyStr); //get response NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"Response Code: %ld", (long)[urlResponse statusCode]); if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) { NSLog(@"Response: %@", result); }}
2、xml的post传递,不传参
-(void) postxml{ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:URLstr]]; [request setHTTPMethod:@"POST"];//声明请求为POST请求 //set headers NSString *contentType = [NSString stringWithFormat:@"text/xml"];//Content-Type数据类型设置xml类型 [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; //create the body NSMutableData *postBody = [NSMutableData data]; [postBody appendData:[[NSString stringWithFormat:@"<此处写根节点><此处写报文头节点><reqUserId>123456</reqUserId><authId>LLLL</authId><authPw>123456</authPw></此处写报文头节点>"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"<此处写报文体节点><deviceCode>2223222</deviceCode><appId>FaceQualityCheck</appId><operateSystem>iOS</operateSystem><operateSystemVersion>8.3</operateSystemVersion></此处写报文体节点></此处写根节点>"] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postBody]; NSString *bodyStr = [[NSString alloc] initWithData:postBody encoding:NSUTF8StringEncoding]; NSLog(@"bodyStr: %@ ",bodyStr); //get response NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"Response Code: %ld", (long)[urlResponse statusCode]); if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) { NSLog(@"Response: %@", result); }}
3、xml解析
1.网上搜索GDataXMLNode,下载得到GDataXMLNode.h和GDataXMLNode.m,导入工程。
2.再导入libxml2.tbd库。
3.在工程的“Build Settings”页中找到“Header Search Path”项,添加/usr/include/libxml2"到路径中3.1解析xml文本
NSString *xmlPath = [[NSBundlemainBundle] pathForResource:@"test"ofType:@"xml"];NSString *xmlString = [NSStringstringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncodingerror:nil];GDataXMLDocument *xmlDoc = [[GDataXMLDocumentalloc] initWithXMLString:xmlString options:0 error:nil];GDataXMLElement *xmlEle = [xmlDoc rootElement];NSArray *array = [xmlEle children];NSLog(@"count : %d", [array count]);for (int i = 0; i < [array count]; i++) { GDataXMLElement *ele = [array objectAtIndex:i]; // 根据标签名判断 if ([[ele name] isEqualToString:@"name"]) { // 读标签里面的属性 NSLog(@"name --> %@", [[ele attributeForName:@"value"] stringValue]); } else { // 直接读标签间的String NSLog(@"age --> %@", [ele stringValue]); } }
}
3.2解析返回数据,
responseData是返回的data
GDataXMLDocument *document=[[GDataXMLDocument alloc]initWithData:responseData options:kNilOptions error:nil];GDataXMLElement *root=document.rootElement;NSArray *arr1=root.children;for (int i = 0; i < [arr1 count]; i++) { GDataXMLElement *ele = [array objectAtIndex:i]; // 根据标签名判断 if ([[ele name] isEqualToString:@"name"]) { // 读标签里面的属性 NSLog(@"name --> %@", [[ele attributeForName:@"value"] stringValue]); } else { // 直接读标签间的String NSLog(@"age --> %@", [ele stringValue]); } }
4、百度上搜集的常见页面访问返回的信息意义:
400 错误请求 — 请求中有语法问题,或不能满足请求。
404 找不到 — 服务器找不到给定的资源;文件不存在
500 内部错误 — 因为意外情况,服务器不能完成请求 或者出问题了
2xx 成功
200 正常;请求已完成。
201 正常;紧接 POST 命令。
202 正常;已接受用于处理,但处理尚未完成。
203 正常;部分信息 — 返回的信息只是一部分。
204 正常;无响应 — 已接收请求,但不存在要回送的信息。
3xx 重定向
301 已移动 — 请求的数据具有新的位置且更改是永久的。
302 已找到 — 请求的数据临时具有不同 URI。
303 请参阅其它 — 可在另一 URI 下找到对请求的响应,且应使用 GET 方法检索此响应。
304 未修改 — 未按预期修改文档。
305 使用代理 — 必须通过位置字段中提供的代理来访问请求的资源。
306 未使用 — 不再使用;保留此代码以便将来使用。
4xx 客户机中出现的错误
400 错误请求 — 请求中有语法问题,或不能满足请求。
401 未授权 — 未授权客户机访问数据。
402 需要付款 — 表示计费系统已有效。
403 禁止 — 即使有授权也不需要访问。
404 找不到 — 服务器找不到给定的资源;文档不存在。
407 代理认证请求 — 客户机首先必须使用代理认证自身。
415 介质类型不受支持 — 服务器拒绝服务请求,因为不支持请求实体的格式。
5xx 服务器中出现的错误
500 内部错误 — 因为意外情况,服务器不能完成请求。
501 未执行 — 服务器不支持请求的工具。
502 错误网关 — 服务器接收到来自上游服务器的无效响应。
503 无法获得服务 — 由于临时过载或维护,服务器无法处理请求。
- iOS实现xml的post传递,返回xml数据进行解析
- iOS实现xml的post传递,返回xml数据进行解析
- WebService传递XML数据 C#DataSet操作XML 解析WebService返回的XML数据
- iphone实现xml的post传递,并且接收数据
- iphone实现xml的post传递,并且接收数据
- iPhone实现XML的POST传递并接收数据
- 解析HTTP POST方式请求的xml数据、本地XML文件数据, 并返回JSON字符串
- php 调用接口的post方法,还有解析返回的xml数据
- springmvc 解析url 返回的xml数据
- 接收POST数据,解析XML
- 调用webservice,解析返回数据为xml格式的字符串,进行数据绑定
- iOS xml数据解析
- iOS解析xml数据
- Android通过post请求发送一个xml,解析返回xml数据
- 网络数据解析(解析返回的xml数据形式)
- 客户端POST提交、服务器返回Xml格式数据的demo
- Http Post Xml形式传递数据
- iOS的Plist-XML-JSON数据解析
- ov9650摄像头驱动之——linux内核v4l2架构分析1
- solution Of 1107. Social Clusters (30)
- 【智力题】—— 猴子分桃
- 【汇编】将ASCII码表示的十进制数转换成二进制数
- 为什么在.html文件中嵌入php代码没效果
- iOS实现xml的post传递,返回xml数据进行解析
- java 生成pdf文件,添加图片
- python释放内存(2)
- Python 获得命令行参数的方法
- angularJS初识--介绍基本功能及用法
- 你应该知道的 RPC 原理
- How to GET a Cup of Coffee
- xib 自定义UITableViewCell 位置滑动错乱解决
- linux kernel的中断子系统之(三):IRQ number和中断描述符