凡是用get方法的,url里含中文的,都需要调用上面的函数进行编码。要不然会被当成二进制截断。
//URL编码
+(NSString*)urlEncode:(NSString *)str
{
int length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
const char* buffer = [str UTF8String];
NSMutableString* returndata = [NSMutableString string];
for(int i=0;i<length;i++)
{
unsigned char ch = (unsigned char)buffer;
[returndata appendFormat:@"%%%02x",ch];
}
return returndata;
}
//URL解码
+(NSString*)urlDecode:(NSString*)str
{
return [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}