今天写一个测试demo,遇到如下问题:
Error Domain=ASIHTTPRequestErrorDomain Code=5 "Unable to create request (bad url?)" UserInfo=0x69ba0f0 {NSLocalizedDescription=Unable to create request (bad url?)}
功能是为了测试分包下载,大概流程:请求URL,服务器返回一套URLList,自己分别去请求每个url最后把得到得数据进行拼接。
每个url如下:
http://sandbox.kaka.com/zinx-nj/%2Fbook%2Fb663c2f03f7b36f4f830979e63f2b0%2F20m_by_5m%2Fmain_1.zip?sign=MBOT:DIoOACjY2prt9jy4i84mGUps9qF941:9BRgIQ1btIaa5CZpZ94Mwm42vE%3D&time=1402132737
在得到URLList以后,对每一个url进行请求得时候,就发生了这样得错误,但是把链接复制到浏览器可以正常使用,还有就是如果把链接复制出来,在代码中一字符串名文得方式指定也可以:
NSString *urlString = @"http://sandbox.kaka.com/zinx-nj/%2Fbook%2Fb663c2f03f7b36f4f830979e63f2b0%2F20m_by_5m%2Fmain_1.zip?sign=MBOT:DIoOACjY2prt9jy4i84mGUps9qF941:9BRgIQ1btIaa5CZpZ94Mwm42vE%3D&time=1402132737"; [self rquest:[NSURL URLWithString:urlString]];
上网查了,大多数人是说其中有中文,需要使用:
url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
但是很显眼在以上url中没有中文,结果在网上找了各种方法试都无效,最后解决方法:
NSString *temp = [URLString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSArray *array = [temp componentsSeparatedByString:@"?"]; NSString * encodedString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge_retained CFStringRef)array[1], NULL, (CFStringRef)@"!*’();:@&=+$,/?%#[]", kCFStringEncodingUTF8); NSString *resultURL = [NSString stringWithFormat:@"%@?%@",array[0],encodedString]; [self request:[NSURL URLWithString:resultURL]];
Unable to create request (bad url?) 解决方案,布布扣,bubuko.com
Unable to create request (bad url?) 解决方案
原文:http://www.cnblogs.com/easy-coding/p/3725109.html