Urlencode in Objective-C

别说谁变了你拦得住时间么 提交于 2019-12-28 06:39:09

问题


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 ?!


回答1:


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.



来源:https://stackoverflow.com/questions/20777112/urlencode-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!