-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpHelper.m
executable file
·336 lines (265 loc) · 12.5 KB
/
HttpHelper.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//
// HttpHelper.m
// HttpHelper
//
// Created by Michael on 9/18/12.
// Copyright (c) 2012 happyMedium
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "HttpHelper.h"
@implementation HttpHelper
@synthesize error,response,method,fileParams,formParams,request,url;
#pragma mark - class methods
#pragma mark - simple send methods
+(NSData *) sendGetRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params
{
HttpHelper * shttp = [[HttpHelper alloc] init];
return [shttp sendGetRequestToURL:aUrl withParameters:params];
}
+(void)sendAsyncGetRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andCompletionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
{
HttpHelper * shttp = [[HttpHelper alloc] init];
[shttp sendAsyncGetRequestToURL:aUrl withParameters:params andCompletionHandler:handler];
}
+(NSData *) sendPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params
{
HttpHelper * shttp = [[HttpHelper alloc] init];
return [shttp sendPostRequestToURL:aUrl withParameters:params];
}
+(void) sendAsyncPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andCompletionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
{
HttpHelper * shttp = [[HttpHelper alloc] init];
[shttp sendAsyncPostRequestToURL:aUrl withParameters:params andCompletionHandler:handler];
}
+(NSData *) sendPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andFiles:(NSDictionary *)files
{
HttpHelper * shttp = [[HttpHelper alloc] init];
return [shttp sendPostRequestToURL:aUrl withParameters:params andFiles:files];
}
+(void) sendAsyncPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andFiles:(NSDictionary *)files andCompletionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
{
HttpHelper * shttp = [[HttpHelper alloc] init];
[shttp sendAsyncPostRequestToURL:aUrl withParameters:params andFiles:files andCompletionHandler:handler];
}
#pragma mark - instance methods
#pragma mark - lifecycle
-(id)init
{
self = [super init];
if(self)
{
self.method = @"POST";
}
return self;
}
-(void)dealloc
{
[self setError:nil];
[self setRequest:nil];
[self setResponse:nil];
[self setMethod:nil];
[self setFileParams:nil];
[self setFormParams:nil];
[self setUrl:nil];
}
#pragma mark - simple send methods
/* these methods perform simple GET/POST requests.
params is a set of key->value pairs corresponding to the form data you would like to send.
*/
-(NSData *) sendGetRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params
{
return [self sendRequest:[self buildGetRequestToURL:aUrl withParameters:params]];
}
-(void) sendAsyncGetRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andCompletionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
{
[self sendAsyncRequest:[self buildGetRequestToURL:aUrl withParameters:params] withCompletionHandler:handler];
}
-(NSData *) sendPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params
{
return [self sendRequest:[self buildPostRequestToURL:aUrl withParameters:params]];
}
-(void) sendAsyncPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andCompletionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
{
[self sendAsyncRequest:[self buildPostRequestToURL:aUrl withParameters:params] withCompletionHandler:handler];
}
-(NSData *) sendPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andFiles:(NSDictionary *)files
{
return [self sendRequest:[self buildPostRequestToURL:aUrl withParameters:params andFiles:files]];
}
-(void) sendAsyncPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andFiles:(NSDictionary *)files andCompletionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
{
[self sendAsyncRequest:[self buildPostRequestToURL:aUrl withParameters:params andFiles:files] withCompletionHandler:handler];
}
#pragma mark - more complex request building methods
-(BOOL) addValue:(id)value forQueryParam:(NSString *)param
{
if(formParams == nil)
{
formParams = [NSMutableDictionary dictionary];
}
[formParams setObject:value forKey:param];
return true;
}
-(BOOL) addFileName:(NSString *)fileName forQueryParam:(NSString *)param
{
if(fileParams == nil)
{
fileParams = [NSMutableDictionary dictionary];
}
if([[method uppercaseString] isEqual:@"GET"])
{
NSLog(@"addFileName error: cannot send file with GET request");
return false;
}
[fileParams setObject:fileName forKey:param];
return true;
}
-(NSData *) send
{
if(fileParams == nil || [[fileParams allKeys] count] == 0) //no files, just parameters
{
if([[method uppercaseString] isEqual:@"POST"])
{
return [self sendRequest:[self buildPostRequestToURL:url withParameters:formParams]];
}
else if([[method uppercaseString]isEqual:@"GET"])
{
return [self sendRequest:[self buildGetRequestToURL:url withParameters:formParams]];
}
else
{
NSLog(@"sendRequest error: unknown method: %@", method);
return nil;
}
}
else //files exist
{
return [self sendRequest:[self buildPostRequestToURL:url withParameters:formParams andFiles:fileParams]];
}
}
-(void) sendAsync:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
{
if(fileParams == nil || [[fileParams allKeys] count] == 0) //no files, just parameters
{
if([[method uppercaseString] isEqual:@"POST"])
{
[self sendAsyncRequest:[self buildPostRequestToURL:url withParameters:formParams] withCompletionHandler:handler];
return;
}
else if([[method uppercaseString]isEqual:@"GET"])
{
[self sendAsyncRequest:[self buildGetRequestToURL:url withParameters:formParams] withCompletionHandler:handler];
return;
}
else
{
NSLog(@"sendRequest error: unknown method: %@", method);
return;
}
}
else //files exist
{
[self sendAsyncRequest:[self buildPostRequestToURL:url withParameters:formParams andFiles:fileParams] withCompletionHandler:handler];
return;
}
}
#pragma mark - sending requests
/* both send methods set response and error automatically, but it's up to you to catch the data sent back by AsyncRequest */
-(NSData *) sendRequest:(NSURLRequest *) aRequest
{
NSError * connError;
NSURLResponse * connResponse;
NSData * data = [NSURLConnection sendSynchronousRequest:aRequest returningResponse:&connResponse error:&connError];
response = connResponse;
error = connError;
return data;
}
-(void) sendAsyncRequest:(NSURLRequest *) aRequest withCompletionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
{
[NSURLConnection sendAsynchronousRequest:aRequest queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *connResponse, NSData *data, NSError *connError) {
response = connResponse;
error = connError;
handler(connResponse, data, connError);
}];
}
#pragma mark - helpers
-(NSMutableURLRequest *) buildPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params andFiles:(NSDictionary *) files
{
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSMutableData * postData = [NSMutableData data];
for (NSString *param in [params allKeys]) {
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"%@\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSMutableData * fileData = [NSMutableData data];
for(NSString * paramName in [files allKeys])
{
NSString * filePath = [files objectForKey:paramName];
NSString * fileName = [filePath lastPathComponent];
NSData * currentFile = [[NSData alloc] initWithContentsOfFile:filePath];
if(!currentFile)
{
NSLog(@"Error: error loading file: %@", filePath);
continue;
}
[fileData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", paramName, fileName ] dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:currentFile];
[fileData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
[fileData appendData:postData];
NSData * allFormData = fileData;
NSString *postLength = [NSString stringWithFormat:@"%d", [allFormData length]];
NSMutableURLRequest *newRequest = [[NSMutableURLRequest alloc] init];
[newRequest setURL:[NSURL URLWithString:aUrl]];
[newRequest setHTTPMethod:@"POST"];
[newRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[newRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
[newRequest setHTTPBody:allFormData];
return newRequest;
}
-(NSMutableURLRequest *) buildPostRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params
{
NSString *post = [self getParamStringFromParams:params];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *newRequest = [[NSMutableURLRequest alloc] init];
[newRequest setURL:[NSURL URLWithString:aUrl]];
[newRequest setHTTPMethod:@"POST"];
[newRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[newRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[newRequest setHTTPBody:postData];
return newRequest;
}
-(NSMutableURLRequest *) buildGetRequestToURL:(NSString *)aUrl withParameters:(NSDictionary *)params
{
NSString * finishedUrl = aUrl;
NSString * paramString = [self getParamStringFromParams:params];
if(![paramString isEqual:@""])
{
finishedUrl = [finishedUrl stringByAppendingFormat:@"?%@", paramString];
}
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:finishedUrl]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];
return newRequest;
}
-(NSString *) getParamStringFromParams:(NSDictionary *)params
{
NSString * paramString = @"";
for(NSString * key in [params allKeys])
{
paramString = [paramString stringByAppendingFormat:@"&%@=%@", key, [params objectForKey:key]];
}
paramString = [paramString substringFromIndex:1]; //remove first ampersand
return paramString;
}
@end