不受支持的urliOS
我有一个有效的url,我得到 – 不受支持的url错误。 可以告诉我为什么?
正如你可以看到有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编码文档的字符集
- 将curl命令转换为iOS
- 如何在UIWebView请求中添加自定义HTTP头,我的UIWebView基于Cordova项目?
- Xcode 7.1在iOS 9中NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813)
- 如何使用AFNetworking库与服务器进行HTTP持久连接
- 使stringWithContentsOfURLasynchronous – 是否安全?
- 通过HTTP POST发送大文件的正确方式是什么,而不是将整个文件加载到内存中?
- 在UIWebView中使用自定义URLscheme
- 如何使用HTTP标头发送非英文unicodestring?
- 将信用卡号码从iOS发送到REST API时的最佳安全实践