Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

utf 8 - Urlencode in Objective-C

i'm trying to make url encode for long string by using this function

    NSData   *PostData   = [MyString dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxxxxx.com/save.php"]];

   [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
   [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
   [request setHTTPBody:PostData];
   [request setHTTPMethod:@"POST"];

but i miss some data when save it to my sql for example if we have this string which contain special characters

("Text !@# & ( | 'l;,. ??????") and < smile faces

when this string receive to my server i miss string after this & and after smile faces ?!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't show us how you created the MyData variable or what type it is, but I'm guessing it's a NSString that you might have constructed something like the following:

NSString *myString = [NSString stringWithFormat:@"%@=%@", parameterKey, parameterValue];
NSData   *postData = [myString dataUsingEncoding:NSUTF8StringEncoding];

What you need to do is to "percent escape" any characters that are defined as reserved per RFC 3986. Thus, you'd replace the above with:

NSString *myString = [NSString stringWithFormat:@"%@=%@", parameterKey, [self percentEScapeString:parameterValue]];
NSData   *postData = [myString dataUsingEncoding:NSUTF8StringEncoding];

where

- (NSString *)percentEscapeString:(NSString *)string
{ 
    return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                     (CFStringRef)string,
                                                                     NULL,
                                                                     (CFStringRef)@":/?@!$&'()*+,;=",
                                                                     kCFStringEncodingUTF8));
}

Technically, per the W3C specs for application/x-www-form-urlencoded, you should replace spaces with + characters, thus:

- (NSString *)percentEscapeString:(NSString *)string
{ 
    NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                 (CFStringRef)string,
                                                                                 (CFStringRef)@" ",
                                                                                 (CFStringRef)@":/?@!$&'()*+,;=",
                                                                                 kCFStringEncodingUTF8));
    return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

Personally, I put these sorts of methods in a NSString category, rather than in the current class, but hopefully this illustrates the idea.

Regardless, do not be tempted to use stringByAddingPercentEscapesUsingEncoding, because that doesn't give you the control you need. You really need to use CFURLCreateStringByAddingPercentEscapes, as shown above.


You asked about the emoticons. The above percent escaping works fine with the emoticons. For example, consider:

NSString *string1 = @"Text !@# & ( | 'l;,. ??????";
NSString *string2 = [self percentEscapeString:string1];
NSString *string3 = [string2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", string1);
NSLog(@"%@", string2);
NSLog(@"%@", string3);
2013-12-25 18:20:16.391 PercentEncodeTest[67199:70b] Text !@# & ( | 'l;,. ??????
2013-12-25 18:20:16.397 PercentEncodeTest[67199:70b] Text%20%21%40%23%20%26%20%28%20%7C%20%27l%3B%2C.%20%F0%9F%98%92%F0%9F%98%9A%F0%9F%98%9C
2013-12-25 18:20:16.401 PercentEncodeTest[67199:70b] Text !@# & ( | 'l;,. ??????

As you can see, string2 is entirely percent escaped and should be correctly transmitted. And when we convert back to NSUTF8StringEncoding, we get our emoticons back fine.

I suspect that the problem now is not in the fact that the emoticons are correctly percent escaped, but rather your use of them by the destination.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...