iOS实现xml的post传递,返回xml数据进行解析


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.hGDataXMLNode.m,导入工程。

2.再导入libxml2.tbd库。

3.在工程的“Build Settings”页中找到“Header Search Path”项,添加/usr/include/libxml2"到路径中
4.编译,如工程能编译通过,则说明GDataXMLNode添加成功

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 无法获得服务 — 由于临时过载或维护,服务器无法处理请求。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值