Created
October 19, 2011 15:01
-
-
Save CastIrony/1298546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)submitPostRequestWithUrl:(NSURL*)url | |
{ | |
NSString* boundary = @"---------------------------14737809831466499882746641449"; | |
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:self.timeOut]; | |
[request setHTTPMethod:@"POST"]; | |
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"]; | |
NSData* postbody = [NSData dataForPOSTWithDictionary:self.postParameters boundary:boundary]; | |
[request setHTTPBody:postbody]; | |
self.request = request; | |
[self submitRequest]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+(NSData*)dataForPOSTWithDictionary:(NSDictionary *)dictionary boundary:(NSString *)boundary | |
{ | |
NSMutableData* postBodyData = [NSMutableData data]; | |
for(NSString* key in dictionary) | |
{ | |
id value = [dictionary valueForKey:key]; | |
if([value isKindOfClass:[NSData class]]) | |
{ | |
[postBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", key, key] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postBodyData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postBodyData appendData:value]; | |
} | |
else | |
{ | |
[postBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postBodyData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postBodyData appendData:[[NSString stringWithFormat:@"%@", value] dataUsingEncoding:NSUTF8StringEncoding]]; | |
} | |
[postBodyData appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
} | |
[postBodyData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
return postBodyData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment