-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNSMutableURLRequest+Posts.m
More file actions
93 lines (77 loc) · 3.25 KB
/
NSMutableURLRequest+Posts.m
File metadata and controls
93 lines (77 loc) · 3.25 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
//
// NSMutableURLRequest+Posts.m
//
// Created by Jiva DeVoe on 3/20/10.
// Copyright 2010 Random Ideas, LLC. All rights reserved.
//
#import "NSMutableURLRequest+Posts.h"
#import "NSData+Base64.h"
@implementation NSMutableURLRequest (Posts)
+(id)postRequestForURL:(NSURL *)inURL withXml:(NSString *)inXml;
{
id req = [self requestWithURL:inURL];
NSString *msgLength = [NSString stringWithFormat:@"%d", [inXml length]];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:[inXml dataUsingEncoding:NSUTF8StringEncoding]];
return req;
}
+(id)postRequestForURL:(NSURL *)inURL withJSON:(NSString *)inJSON;
{
id req = [self requestWithURL:inURL];
NSString *msgLength = [NSString stringWithFormat:@"%d", [inJSON length]];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:[inJSON dataUsingEncoding:NSUTF8StringEncoding]];
return req;
}
+(id)requestWithURL:(NSURL *)inURL params:(NSDictionary *)inDictionary;
{
id req = [self requestWithURL:inURL];
NSMutableString *paramString = [NSMutableString string];
for(NSString *key in [inDictionary allKeys])
{
[paramString appendString:[NSString stringWithFormat:@"%@=%@&", key, [inDictionary objectForKey:key]]];
}
NSString *msgLength = [NSString stringWithFormat:@"%d", [paramString length]];
[req addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:[paramString dataUsingEncoding:NSUTF8StringEncoding]];
return req;
}
+(id)postRequestForURL:(NSURL *)inURL params:(NSDictionary *)inDictionary;
{
id req = [self requestWithURL:inURL params:inDictionary];
[req setHTTPMethod:@"POST"];
return req;
// id req = [self requestWithURL:inURL];
//
// NSMutableString *paramString = [NSMutableString string];
//
// for(NSString *key in [inDictionary allKeys])
// {
// [paramString appendString:[NSString stringWithFormat:@"%@=%@&", key, [inDictionary objectForKey:key]]];
// }
//
// NSString *msgLength = [NSString stringWithFormat:@"%d", [paramString length]];
// [req addValue: msgLength forHTTPHeaderField:@"Content-Length"];
// [req setHTTPMethod:@"POST"];
// [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
// [req setHTTPBody:[paramString dataUsingEncoding:NSUTF8StringEncoding]];
// return req;
}
+(id)putRequestForURL:(NSURL *)inURL params:(NSDictionary *)inDictionary;
{
id req = [self requestWithURL:inURL params:inDictionary];
[req setHTTPMethod:@"PUT"];
return req;
}
-(void)addAuthenticationForUsername:(NSString *)inUsername password:(NSString *)inPassword
{
NSString *tmp = [NSString stringWithFormat:@"%@:%@", inUsername, inPassword];
NSString *data = [[NSData dataWithBytes:[tmp UTF8String] length:[tmp length]] base64Encoding];
[self addValue:[NSString stringWithFormat:@"Basic %@", data] forHTTPHeaderField:@"Authorization"];
}
@end