这几天遇到问题,就是有关数据的交互时候,代码和上传的东西都是正确的但是后台报错了
在此记录一下,提供大家方便。
是关于转码的:
+ (NSString *)getAuthToken{
NSString *timestamp = [NSDate stringFromDate:[NSDate date] format:@"yyyyMMddHHmmss"];
NSString *encryptString = [[NSString stringWithFormat:@"atom|%@",timestamp] encryptStringWithKey:nil];
return [encryptString stringByURLEncodingStringParameter];
}
[encryptString stringByURLEncodingStringParameter]; 这个 转换 utf8 试试
- (NSString *)stringByURLEncodingStringParameter
{
// NSURL's stringByAddingPercentEscapesUsingEncoding: does not escape
// some characters that should be escaped in URL parameters, like / and ?;
// we'll use CFURL to force the encoding of those
//
// We'll explicitly leave spaces unescaped now, and replace them with +'s
//
// Reference: <a href="\"http://www.ietf.org/rfc/rfc3986.txt\"" target="\"_blank\"" οnclick="\"return" checkurl(this)\"="" id="\"url_2\"">http://www.ietf.org/rfc/rfc3986.txt</a>
NSString *resultStr = self;
CFStringRef originalString = (CFStringRef)self;
CFStringRef leaveUnescaped = CFSTR(" ");
CFStringRef forceEscaped = CFSTR("!*'();:@&=+$,/?%#[]");
CFStringRef escapedStr;
escapedStr = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
originalString,
leaveUnescaped,
forceEscaped,
kCFStringEncodingUTF8);
if (escapedStr) {
NSMutableString *mutableStr = [NSMutableString stringWithString:(NSString *)escapedStr];
CFRelease(escapedStr);
[mutableStr replaceOccurrencesOfString:@" "
withString :@"%20"
options :0
range :NSMakeRange(0, [mutableStr length])];
resultStr = mutableStr;
}
return resultStr;
}
// URL编码加强版,解决特殊字符,比如 + 空格 等
- (NSString *)stringByURLEncodingStringParameter;