-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimplePost.m
More file actions
101 lines (83 loc) · 4.76 KB
/
Copy pathSimplePost.m
File metadata and controls
101 lines (83 loc) · 4.76 KB
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
//
// SimplePost.m
// SimplePost
//
// Created by Nicolas Goles on 10/12/11.
// Licensed under the MIT license
//
#import "SimplePost.h"
@implementation SimplePost
/** Creates a multipart HTTP POST request.
* @param url is the target URL for the POST request
* @param dictionary is a key/value dictionary with the DATA of the multipart post.
*
* Should be constructed like:
* NSArray *keys = [[NSArray alloc] initWithObjects:@"login", @"password", nil];
* NSArray *objects = [[NSArray alloc] initWithObjects:@"TheLoginName", @"ThePassword!", nil];
* NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
*/
+ (NSMutableURLRequest *) multipartRequestWithURL:(NSURL *)url andDataDictionary:(NSDictionary *) dictionary
{
// Create POST request
NSMutableURLRequest *mutipartPostRequest = [NSMutableURLRequest requestWithURL:url];
[mutipartPostRequest setHTTPMethod:@"POST"];
// Add HTTP header info
// Note: POST boundaries are described here: http://www.vivtek.com/rfc1867.html
// and here http://www.w3.org/TR/html4/interact/forms.html
NSString *POSTBoundary = [NSString stringWithString:@"0xHttPbOuNdArY"]; // You could calculate a better boundary here.
[mutipartPostRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
// Add HTTP Body
NSMutableData *POSTBody = [NSMutableData data];
[POSTBody appendData:[[NSString stringWithFormat:@"--%@\r\n",POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Add Key/Values to the Body
NSEnumerator *enumerator = [dictionary keyEnumerator];
NSString *key;
while ((key = [enumerator nextObject])) {
[POSTBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[POSTBody appendData:[[NSString stringWithFormat:@"%@", [dictionary objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
if (key != nil) {
[POSTBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
// Add the closing -- to the POST Form
[POSTBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Add the body to the mutipartPostRequest & return
[mutipartPostRequest setHTTPBody:POSTBody];
return mutipartPostRequest;
}
/** Creates a form-urlencoded HTTP POST request.
* @param url is the target URL for the POST request
* @param dictionary is a key/value dictionary with the DATA of the multipart post.
*
* Should be constructed like:
* NSArray *keys = [[NSArray alloc] initWithObjects:@"login", @"password", nil];
* NSArray *objects = [[NSArray alloc] initWithObjects:@"TheLoginName", @"ThePassword!", nil];
* NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
*/
+ (NSMutableURLRequest *) urlencodedRequestWithURL:(NSURL *)url andDataDictionary:(NSDictionary *) dictionary
{
// Create POST request
NSMutableURLRequest *urlencodedPostRequest = [NSMutableURLRequest requestWithURL:url];
[urlencodedPostRequest setHTTPMethod:@"POST"];
// Add HTTP header info
[urlencodedPostRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
// Add POST body
NSMutableData *POSTBody = [NSMutableData data];
// Add k/v to the body
NSArray *keyArray = [dictionary allKeys];
for (int i = 0; i < [keyArray count]; ++i) {
// Core Foundation function used to transform @ ==> %40 , etc
NSString *escapedString = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)([dictionary objectForKey:[keyArray objectAtIndex:i]]),
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
[POSTBody appendData:[[NSString stringWithFormat:@"%@=%@", [keyArray objectAtIndex:i], escapedString] dataUsingEncoding:NSUTF8StringEncoding]];
if (i < ([keyArray count] - 1)) {
[POSTBody appendData:[[NSString stringWithFormat:@"&"] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[urlencodedPostRequest setHTTPBody:POSTBody];
return urlencodedPostRequest;
}
@end