forked from sonofsky2010/TQT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQMutableURLRequest.m
93 lines (72 loc) · 3.46 KB
/
QMutableURLRequest.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
//
// QSyncHttp.m
// QWeiboSDK4iOS
//
// Created on 11-1-13.
//
//
#import "QMutableURLRequest.h"
#import "NSURL+QAdditions.h"
@implementation QMutableURLRequest
#pragma mark -
#pragma mark calss methods
+ (NSMutableURLRequest *)requestGet:(NSString *)aUrl queryString:(NSString *)aQueryString {
NSMutableString *url = [[NSMutableString alloc] initWithString:aUrl];
if (aQueryString) {
[url appendFormat:@"?%@", aQueryString];
}
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL smartURLForString:url]] autorelease];
[request setHTTPMethod:@"GET"];
[request setTimeoutInterval:20.0f];
[url release];
return request;
}
+ (NSMutableURLRequest *)requestPost:(NSString *)aUrl queryString:(NSString *)aQueryString {
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL smartURLForString:aUrl]] autorelease];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:20.0f];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[aQueryString dataUsingEncoding:NSUTF8StringEncoding]];
return request;
}
+ (NSMutableURLRequest *)requestPostWithFile:(NSDictionary *)files url:(NSString *)aUrl queryString:(NSString *)aQueryString {
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL smartURLForString:aUrl]] autorelease];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:20.0f];
//generate boundary string
CFUUIDRef uuid;
CFStringRef uuidStr;
uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
uuidStr = CFUUIDCreateString(NULL, uuid);
assert(uuidStr != NULL);
NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", uuidStr];
CFRelease(uuidStr);
CFRelease(uuid);
NSData *boundaryBytes = [[NSString stringWithFormat:@"\r\n--%@", boundary] dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
NSMutableData *bodyData = [NSMutableData data];
NSString *formDataTemplate = @"\r\n--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@";
NSDictionary *listParams = [NSURL parseURLQueryString:aQueryString];
for (NSString *key in listParams) {
NSString *value = [listParams valueForKey:key];
NSString *formItem = [NSString stringWithFormat:formDataTemplate, boundary, key, value];
[bodyData appendData:[formItem dataUsingEncoding:NSUTF8StringEncoding]];
}
[bodyData appendData:boundaryBytes];
NSString *headerTemplate = @"\r\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\nContent-Type: \"application/octet-stream\"\r\n\r\n";
for (NSString *key in files) {
NSString *filePath = [files objectForKey:key];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *header = [NSString stringWithFormat:headerTemplate, key, [[filePath componentsSeparatedByString:@"/"] lastObject]];
[bodyData appendData:[header dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:fileData];
[bodyData appendData:boundaryBytes];
}
[bodyData appendData:[[NSString stringWithUTF8String:"--\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// NSLog(@"%@", [[[NSString alloc] initWithData:bodyData encoding:NSUTF8StringEncoding] autorelease]);
[request setValue:[NSString stringWithFormat:@"%d", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:bodyData];
return request;
}
@end