我有一个有效的网址,并且得到了-不支持的网址错误。能告诉我为什么吗?
如您所见,这里有http://
// http://fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065
Error description=Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x78f97920 {NSUnderlyingError=0x79f78bd0 "unsupported URL", NSLocalizedDescription=unsupported URL}
这就是我试图初始化url的方式:
方法1:
NSString *path=@"http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065";
NSURL *url=[NSURL URLWithString:path];
NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:url];
方法2:
NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065"]];
URL不能包含不在ASCII字符集中的字符,这些字符必须被转义。
使用stringByAddingPercentEncodingWithAllowedCharacters
与字符集URLQueryAllowedCharacterSet
NSString *path = @"http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065";
NSString *escapedPath = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"escapedPath: %@", escapedPath);
输出:
escapedPath:http://fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065 \
请参阅字符集以获取URL编码文档
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句