这几天做东西用到了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