XML数据包网络请求

刚来的这家公司项目后台数据是XML,数据请求是是发送XML数据包到后台,在网上找了半天都没有满意的,自己用原生的和AFNetworking试了一下。

原生的代码如下:

NSString *urlString = [NSString stringWithFormat:@"http://xxxxxx"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableString * postString = [NSMutableString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
    [postString appendString:@"<Login>"];
    [postString appendFormat:@"<UserName>%@</UserName>", self.accountTF.text];
    [postString appendFormat:@"<Pwd>%@</Pwd>", self.passwordTF.text];
    [postString appendString:@"</Login>"];
    NSString * postBodyString = [postString copy];
    NSData * postBody = [postBodyString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    [request setHTTPBody:postBody];

    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", [urlResponse statusCode]);
    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
        NSLog(@"Response: %@", result);
    }

AFNetworking的代码如下:

    NSString *urlString = [NSString stringWithFormat:@"http://xxxxxx"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableString * postString = [NSMutableString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
    [postString appendString:@"<Login>"];
    [postString appendFormat:@"
    [postString appendFormat:@"<UserName>%@</UserName>", self.accountTF.text];
    [postString appendFormat:@"<Pwd>%@</Pwd>", self.passwordTF.text];
    [postString appendString:@"</Login>"];
    NSString * postBodyString = [postString copy];
    NSData * postBody = [postBodyString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    [request setHTTPBody:postBody];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
    operation.responseSerializer.acceptableContentTypes = [operation.responseSerializer.acceptableContentTypes setByAddingObject:@"text/xml"];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];

    [operation start];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值