中文UrlEncode

  • (NSString *)urlencode:(NSString *)urlStr
    {
    NSMutableString *output = [NSMutableString string];
    const unsigned char *source = (const unsigned char *)[urlStr UTF8String];
    int sourceLen = strlen((const char *)source);
    for (int i = 0; i < sourceLen; ++i) {
    const unsigned char thisChar = 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;
    }

你可能感兴趣的:(中文UrlEncode)