ios用post请求xml数据

这几天做东西用到了post请求xml数据,然后整理了一下。

1 创建一个类

#import<Foundation/Foundation.h>

@interface MYXml : NSObject

- (void)MYXml:(NSDictionary*)params :(void(^)(NSDictionary*resultDic))result;

@end


#import "MYXml.h"

@implementation JYDPaySDK

-(void)MYXml:(NSDictionary *)params :(void (^)(NSDictionary *))result{    NSString *urlString =JYDURL;    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"];    NSMutableData *postBody = [NSMutableData data];    [postBody appendData:[[NSString stringWithFormat:@""] dataUsingEncoding:NSUTF8StringEncoding]];    [postBody appendData:[[NSString stringWithFormat:@"%@",params[@"service"]] dataUsingEncoding:NSUTF8StringEncoding]];    [postBody appendData:[[NSString stringWithFormat:@"%@",params[@"version"]] dataUsingEncoding:NSUTF8StringEncoding]];

[postBody appendData:[[NSString stringWithFormat:@""] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postBody];

NSHTTPURLResponse* urlResponse = nil;

NSError *error = [[NSError alloc] init];

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {

得到返回结果,如果是xml格式,使用这个XMLReader github.com/amarcadet/XMLReader是非常不错的选择

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容