Showing
2 changed files
with
34 additions
and
23 deletions
| @@ -24,10 +24,12 @@ typedef enum _ASIPostFormat { | @@ -24,10 +24,12 @@ typedef enum _ASIPostFormat { | ||
| 24 | NSMutableDictionary *fileData; | 24 | NSMutableDictionary *fileData; |
| 25 | 25 | ||
| 26 | ASIPostFormat postFormat; | 26 | ASIPostFormat postFormat; |
| 27 | + | ||
| 28 | + NSStringEncoding stringEncoding; | ||
| 27 | } | 29 | } |
| 28 | 30 | ||
| 29 | #pragma mark utilities | 31 | #pragma mark utilities |
| 30 | -+ (NSString*) encodeURL:(CFStringRef) string; | 32 | +- (NSString*)encodeURL:(NSString *)string; |
| 31 | 33 | ||
| 32 | #pragma mark setup request | 34 | #pragma mark setup request |
| 33 | 35 | ||
| @@ -48,4 +50,5 @@ typedef enum _ASIPostFormat { | @@ -48,4 +50,5 @@ typedef enum _ASIPostFormat { | ||
| 48 | 50 | ||
| 49 | 51 | ||
| 50 | @property (assign) ASIPostFormat postFormat; | 52 | @property (assign) ASIPostFormat postFormat; |
| 53 | +@property (assign) NSStringEncoding stringEncoding; | ||
| 51 | @end | 54 | @end |
| @@ -20,23 +20,28 @@ | @@ -20,23 +20,28 @@ | ||
| 20 | @implementation ASIFormDataRequest | 20 | @implementation ASIFormDataRequest |
| 21 | 21 | ||
| 22 | #pragma mark utilities | 22 | #pragma mark utilities |
| 23 | -+ (NSString*) encodeURL:(CFStringRef)string | 23 | +- (NSString*)encodeURL:(NSString *)string |
| 24 | { | 24 | { |
| 25 | - CFStringRef result = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, | 25 | + NSString *newString = [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])) autorelease]; |
| 26 | - string, | 26 | + if (newString) { |
| 27 | - NULL, | 27 | + return newString; |
| 28 | - CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), | 28 | + } |
| 29 | - kCFStringEncodingUTF8); | 29 | + return @""; |
| 30 | - return [(NSString*) result autorelease]; | ||
| 31 | } | 30 | } |
| 32 | 31 | ||
| 33 | #pragma mark init / dealloc | 32 | #pragma mark init / dealloc |
| 34 | 33 | ||
| 35 | + (id)requestWithURL:(NSURL *)newURL | 34 | + (id)requestWithURL:(NSURL *)newURL |
| 36 | { | 35 | { |
| 37 | - ASIFormDataRequest *request = [[[self alloc] initWithURL:newURL] autorelease]; | 36 | + return [[[self alloc] initWithURL:newURL] autorelease]; |
| 38 | - [request setPostFormat:ASIMultipartFormDataPostFormat]; | 37 | +} |
| 39 | - return request; | 38 | + |
| 39 | +- (id)initWithURL:(NSURL *)newURL | ||
| 40 | +{ | ||
| 41 | + self = [super initWithURL:newURL]; | ||
| 42 | + [self setPostFormat:ASIMultipartFormDataPostFormat]; | ||
| 43 | + [self setStringEncoding:NSUTF8StringEncoding]; | ||
| 44 | + return self; | ||
| 40 | } | 45 | } |
| 41 | 46 | ||
| 42 | - (void)dealloc | 47 | - (void)dealloc |
| @@ -134,21 +139,23 @@ | @@ -134,21 +139,23 @@ | ||
| 134 | 139 | ||
| 135 | - (void)buildMultipartFormDataPostBody | 140 | - (void)buildMultipartFormDataPostBody |
| 136 | { | 141 | { |
| 142 | + NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); | ||
| 143 | + | ||
| 137 | // Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. | 144 | // Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. |
| 138 | NSString *stringBoundary = @"0xKhTmLbOuNdArY"; | 145 | NSString *stringBoundary = @"0xKhTmLbOuNdArY"; |
| 139 | 146 | ||
| 140 | - [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; charset=UTF-8; boundary=%@",stringBoundary]]; | 147 | + [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary]]; |
| 141 | 148 | ||
| 142 | - [self appendPostData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; | 149 | + [self appendPostData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:[self stringEncoding]]]; |
| 143 | 150 | ||
| 144 | // Adds post data | 151 | // Adds post data |
| 145 | - NSData *endItemBoundary = [[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]; | 152 | + NSData *endItemBoundary = [[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:[self stringEncoding]]; |
| 146 | NSEnumerator *e = [[self postData] keyEnumerator]; | 153 | NSEnumerator *e = [[self postData] keyEnumerator]; |
| 147 | NSString *key; | 154 | NSString *key; |
| 148 | int i=0; | 155 | int i=0; |
| 149 | while (key = [e nextObject]) { | 156 | while (key = [e nextObject]) { |
| 150 | - [self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]]; | 157 | + [self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key] dataUsingEncoding:[self stringEncoding]]]; |
| 151 | - [self appendPostData:[[[self postData] objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding]]; | 158 | + [self appendPostData:[[[self postData] objectForKey:key] dataUsingEncoding:[self stringEncoding]]]; |
| 152 | i++; | 159 | i++; |
| 153 | if (i != [[self postData] count] || [[self fileData] count] > 0) { //Only add the boundary if this is not the last item in the post body | 160 | if (i != [[self postData] count] || [[self fileData] count] > 0) { //Only add the boundary if this is not the last item in the post body |
| 154 | [self appendPostData:endItemBoundary]; | 161 | [self appendPostData:endItemBoundary]; |
| @@ -164,8 +171,8 @@ | @@ -164,8 +171,8 @@ | ||
| 164 | NSString *contentType = [fileInfo objectForKey:@"contentType"]; | 171 | NSString *contentType = [fileInfo objectForKey:@"contentType"]; |
| 165 | NSString *fileName = [fileInfo objectForKey:@"fileName"]; | 172 | NSString *fileName = [fileInfo objectForKey:@"fileName"]; |
| 166 | 173 | ||
| 167 | - [self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, fileName] dataUsingEncoding:NSUTF8StringEncoding]]; | 174 | + [self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, fileName] dataUsingEncoding:[self stringEncoding]]]; |
| 168 | - [self appendPostData:[[NSString stringWithFormat:@"Content-Type: %@; charset=UTF-8\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]]; | 175 | + [self appendPostData:[[NSString stringWithFormat:@"Content-Type: %@; charset=%@\r\n\r\n", contentType, charset] dataUsingEncoding:[self stringEncoding]]]; |
| 169 | 176 | ||
| 170 | if ([file isKindOfClass:[NSString class]]) { | 177 | if ([file isKindOfClass:[NSString class]]) { |
| 171 | [self appendPostDataFromFile:file]; | 178 | [self appendPostDataFromFile:file]; |
| @@ -179,7 +186,7 @@ | @@ -179,7 +186,7 @@ | ||
| 179 | } | 186 | } |
| 180 | } | 187 | } |
| 181 | 188 | ||
| 182 | - [self appendPostData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; | 189 | + [self appendPostData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:[self stringEncoding]]]; |
| 183 | 190 | ||
| 184 | } | 191 | } |
| 185 | 192 | ||
| @@ -191,8 +198,9 @@ | @@ -191,8 +198,9 @@ | ||
| 191 | [self buildMultipartFormDataPostBody]; | 198 | [self buildMultipartFormDataPostBody]; |
| 192 | return; | 199 | return; |
| 193 | } | 200 | } |
| 201 | + NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); | ||
| 194 | 202 | ||
| 195 | - [self addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded; charset=UTF-8"]; | 203 | + [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@",charset]]; |
| 196 | 204 | ||
| 197 | 205 | ||
| 198 | NSEnumerator *e = [[self postData] keyEnumerator]; | 206 | NSEnumerator *e = [[self postData] keyEnumerator]; |
| @@ -200,8 +208,8 @@ | @@ -200,8 +208,8 @@ | ||
| 200 | int i=0; | 208 | int i=0; |
| 201 | int count = [[self postData] count]-1; | 209 | int count = [[self postData] count]-1; |
| 202 | while (key = [e nextObject]) { | 210 | while (key = [e nextObject]) { |
| 203 | - NSString *data = [NSString stringWithFormat:@"%@=%@%@", [ASIFormDataRequest encodeURL:(CFStringRef)key], [ASIFormDataRequest encodeURL:(CFStringRef)[[self postData] objectForKey:key]],(i<count ? @"&" : @"")]; | 211 | + NSString *data = [NSString stringWithFormat:@"%@=%@%@", [self encodeURL:key], [self encodeURL:[[self postData] objectForKey:key]],(i<count ? @"&" : @"")]; |
| 204 | - [self appendPostData:[data dataUsingEncoding:NSUTF8StringEncoding]]; | 212 | + [self appendPostData:[data dataUsingEncoding:[self stringEncoding]]]; |
| 205 | i++; | 213 | i++; |
| 206 | } | 214 | } |
| 207 | } | 215 | } |
| @@ -209,5 +217,5 @@ | @@ -209,5 +217,5 @@ | ||
| 209 | @synthesize postData; | 217 | @synthesize postData; |
| 210 | @synthesize fileData; | 218 | @synthesize fileData; |
| 211 | @synthesize postFormat; | 219 | @synthesize postFormat; |
| 212 | - | 220 | +@synthesize stringEncoding; |
| 213 | @end | 221 | @end |
-
Please register or login to post a comment