-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTBXML+HTTP.m
executable file
·115 lines (82 loc) · 3.23 KB
/
TBXML+HTTP.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// TBXML+HTTP.m
//
// Created by Tom Bradley on 29/01/2011.
// Copyright 2012 71Squared All rights reserved.
//
#import "TBXML+HTTP.h"
@implementation NSMutableURLRequest (TBXML_HTTP)
+ (NSMutableURLRequest*) tbxmlGetRequestWithURL:(NSURL*)url {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"GET"];
#ifndef ARC_ENABLED
return [request autorelease];
#else
return request;
#endif
}
+ (NSMutableURLRequest*) tbxmlPostRequestWithURL:(NSURL*)url parameters:(NSDictionary*)parameters {
NSMutableArray * params = [NSMutableArray new];
for (NSString * key in [parameters allKeys]) {
[params addObject:[NSString stringWithFormat:@"%@=%@", key, [parameters objectForKey:key]]];
}
NSData * postData = [[params componentsJoinedByString:@"&"] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
#ifndef ARC_ENABLED
[params release];
return [request autorelease];
#else
return request;
#endif
}
@end
@implementation NSURLConnection (TBXML_HTTP)
+ (void)tbxmlAsyncRequest:(NSURLRequest *)request success:(TBXMLAsyncRequestSuccessBlock)successBlock failure:(TBXMLAsyncRequestFailureBlock)failureBlock {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@autoreleasepool {
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
failureBlock(data,error);
} else {
successBlock(data,response);
}
}
});
}
@end
@implementation TBXML (TBXML_HTTP)
+ (id)newTBXMLWithURL:(NSURL*)aURL success:(TBXMLSuccessBlock)successBlock failure:(TBXMLFailureBlock)failureBlock {
return [[TBXML alloc] initWithURL:aURL success:successBlock failure:failureBlock];
}
- (id)initWithURL:(NSURL*)aURL success:(TBXMLSuccessBlock)successBlock failure:(TBXMLFailureBlock)failureBlock {
self = [self init];
if (self != nil) {
TBXMLAsyncRequestSuccessBlock requestSuccessBlock = ^(NSData *data, NSURLResponse *response) {
NSError *error = nil;
[self decodeData:data withError:&error];
// If TBXML found a root node, process element and iterate all children
if (!error) {
successBlock(self);
} else {
failureBlock(self, error);
}
};
TBXMLAsyncRequestFailureBlock requestFailureBlock = ^(NSData *data, NSError *error) {
failureBlock(self, error);
};
[NSURLConnection tbxmlAsyncRequest:[NSMutableURLRequest tbxmlGetRequestWithURL:aURL]
success:requestSuccessBlock
failure:requestFailureBlock];
}
return self;
}
@end