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 | // // TTURLRequest+Additions.h // circleplus // // Created by GDX-Mac on 2011/7/20. // Copyright 2011å¹´ __MyCompanyName__. All rights reserved. // #import <Three20/Three20.h> @interface TTURLRequest (Additions) + (NSData *)getFormUrlEncodedBodyWithParameter:(NSDictionary *)parameter; - (void)setFormUrlEncoded; @end // // TTURLRequest+Additions.m // circleplus // // Created by GDX-Mac on 2011/7/20. // Copyright 2011å¹´ __MyCompanyName__. All rights reserved. // #import "TTURLRequest+Additions.h" @implementation TTURLRequest (Additions) + (NSData *)getFormUrlEncodedBodyWithParameter:(NSDictionary *)parameters { NSMutableString *postData = [NSMutableString string]; for (NSString *key in [parameters allKeys]) { NSString *value = [[parameters objectForKey:key] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [postData appendFormat:@"%@=%@&", key, value]; } [postData replaceCharactersInRange:NSMakeRange([postData length] - 1, 1) withString:@""]; NSLog(@"%@", postData); return [NSData dataWithBytes:[postData UTF8String] length:[postData length]]; } - (void)setFormUrlEncoded { self.httpMethod = @"POST"; self.contentType=@"application/x-www-form-urlencoded"; } @end // usage TTURLRequest *request = [TTURLRequest requestWithURL:[CPService URLWithPath:kCPServiceURLLogin] delegate:self]; [request setFormUrlEncoded]; request.httpBody = [TTURLRequest getFormUrlEncodedBodyWithParameter:[NSDictionary dictionaryWithObject:value forKey:key]]; |
Direct link: https://paste.plurk.com/show/568027