今天发现一个蛋疼的问题,使用ASIHTTPRequest Get方式请求数据时候带中文,iOS客户端不能正确进行网络请求。 NSURL *url = [NSURL URLWithString:@http://unmi.cc?p1=%+&sd f&p2=中文"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 打印出来url为nil 还好,在网上找到了如下方法就可以解决这个问题 //NSString+URLEncoding.h #import<Foundation/Foundation.h> @interface NSString (OAURLEncodingAdditions) - (NSString *)URLEncodedString; - (NSString *)URLDecodedString; @end //NSString+URLEncoding.m #import"NSString+URLEncoding.h" @implementation NSString (OAURLEncodingAdditions) - (NSString *)URLEncodedString { NSString*result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8); [result autorelease]; return result; } //解决url乱码 - (NSString*)URLDecodedString { NSString*result = (NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8); [result autorelease]; return result; } // 调用方法 NSString *encodedString = [urlString URLEncodedString];