简体   繁体   English

URL的编码字符串

[英]encoding string for URL

I have to POST data to a php page from an Iphone application, so i need to encode the parameters properly, converting the special characters... 我必须将数据从Iphone应用程序发布到php页面,因此我需要正确编码参数,转换特殊字符...

Specifically i need to send the UDID of the iphone. 具体来说,我需要发送iPhone的UDID。

I've found many helps on the web to encode the String to pass as parameter, but i got a strange error. 我在网络上发现了很多帮助将String编码为参数的方法,但是出现了一个奇怪的错误。

I'm using this function: 我正在使用此功能:

- (NSString *)URLEncodeString:(NSString *)string { 

NSString *result =
   (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
    (CFStringRef)string, NULL, CFSTR("'\"?=&+<>;:-"), kCFStringEncodingUTF8);

return [result autorelease];}

it seems correct but when i use it, the result isn't what i expect for. 看来是正确的,但是当我使用它时,结果却不是我所期望的。

This is the code: 这是代码:

UIDevice *device = [UIDevice currentDevice];
NSString *uniqueIdentifier = [device uniqueIdentifier];

NSLog(uniqueIdentifier);
NSLog([self URLEncodeString:uniqueIdentifier]);

and this is the Log generated when i hit this code: 这是当我按下此代码时生成的日志:

2010-06-23 21:58:51.671 provaNavigation[2343:20b] 00000000-0000-1000-8000-0013775CE6D2
2010-06-23 21:58:51.672 provaNavigation[2343:20b] 0000000010003900009080176010001273086780001283520013775CE6D2

and again: 然后再次:

2010-06-23 21:59:25.614 provaNavigation[2343:20b] 00000000-0000-1000-8000-0013775CE6D2
2010-06-23 21:59:25.615 provaNavigation[2343:20b] 000000001000390000908192801000-18000912258750013775CE6D2

and again: 然后再次:

2010-06-23 21:59:40.848 provaNavigation[2343:20b] 00000000-0000-1000-8000-0013775CE6D2
2010-06-23 21:59:40.849 provaNavigation[2343:20b] 000000001000390000908866081000-18000912239630013775CE6D2

and again... 然后再次...

I get a different value every time and never correct with the right %number. 我每次都会得到一个不同的值,并且永远无法使用正确的%number进行校正。

I've come across problems URL encoding before, so I ended up writing a category on NSString to properly encode URLs: 我之前遇到过URL编码问题,所以最终在NSString上编写了一个类别以正确编码URL:

- (NSString *) URLEncodedString {
  NSMutableString * output = [NSMutableString string];
  const char * source = [self UTF8String];
  int sourceLen = strlen(source);
  for (int i = 0; i < sourceLen; ++i) {
    const unsigned char thisChar = (const unsigned char)source[i];
    if (thisChar == ' '){
      [output appendString:@"+"];
    } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || 
           (thisChar >= 'a' && thisChar <= 'z') ||
           (thisChar >= 'A' && thisChar <= 'Z') ||
           (thisChar >= '0' && thisChar <= '9')) {
      [output appendFormat:@"%c", thisChar];
    } else {
      [output appendFormat:@"%%%02X", thisChar];
    }
  }
  return output;
}

- (NSString *) URLDecodedString {
  NSString * spacedDecoded = [self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
  return [spacedDecoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

There are a couple notes about this: 关于此有一些注意事项:

  1. This will encode spaces as "+" 这会将空格编码为“ +”
  2. This encodes everything except az (upper and lowercase), numbers, period, hyphen, underscore, and tilde 这将对除az(大写和小写),数字,句点,连字符,下划线和代字号之外的所有内容进行编码
  3. This will properly encode unicode characters as multiple octets. 这样可以将Unicode字符正确编码为多个八位字节。 For example, I can do: 例如,我可以这样做:

     NSString * enc = [@"http://example.com/?data=÷¡¿" URLEncodedString]; NSString * enc = [@“http://example.com/?data=÷¡¿” URLEncodedString];\nNSLog(@"%@", enc); NSLog(@“%@”,enc);\nNSLog(@"%@", [enc URLDecodedString]); NSLog(@“%@”,[enc URLDecodedString]); 

    Which will properly log: 哪个将正确记录:

     Testing.m:65 main() http%3A%2F%2Fexample.com%2F%3Fdata%3D%C3%B7%C2%A1%C2%BF Testing.m:65 main()http%3A%2F%2Fexample.com%2F%3Fdata%3D%C3%B7%C2%A1%C2%BF\nTesting.m:66 main() http://example.com/?data=÷¡¿ Testing.m:66 main()http://example.com/?data=÷¡¿ 

EDIT I copied and pasted it into an empty project, like this: 编辑我将其复制并粘贴到一个空项目中,如下所示:

NSString * string = @"00000000-0000-1000-8000-0013775CE6D2";
NSString *result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR("'\"?=&+<>;:-"), kCFStringEncodingUTF8);
NSLog(@"encode? %@", result);
result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"decode? %@", result);
NSLog(@"equal? %d", [string isEqual:result]);

And it logs: 它记录:

Testing[62541:a0f] encode? 00000000%2D0000%2D1000%2D8000%2D0013775CE6D2
Testing[62541:a0f] decode? 00000000-0000-1000-8000-0013775CE6D2
Testing[62541:a0f] equal? 1

The reason your code isn't working is because you're logging the string as the format string , which means it's seeing the %2 stuff and trying to replace it with other things. 您的代码无法正常工作的原因是,您将字符串记录为格式字符串 ,这意味着它看到%2东西并试图用其他东西替换它。 Observe: 观察:

NSString * string = @"00000000-0000-1000-8000-0013775CE6D2";
NSString *result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR("'\"?=&+<>;:-"), kCFStringEncodingUTF8);
NSLog(@"%@", result);
NSLog(result);

Logs: 日志:

Testing[62585:a0f] 00000000%2D0000%2D1000%2D8000%2D0013775CE6D2
Testing[62585:a0f] 00000000879923200001897296640100041968968000 00013775CE6D2

So the string is getting properly encoded, but you're just logging it incorrectly. 因此,该字符串已得到正确的编码,但是您只是在错误地记录它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM