单个“|” URL 中的字符会中断 URL 的加载内容

问题描述

我正在尝试从 Wikipedia API 加载 JSON 格式的文章,但出现以下错误:

nil host used in call to allowsSpecificHTTPSCertificateForHost
nil host used in call to allowsAnyHTTPSCertificateForHost:
NSURLConnection finished with error - code -1002
error when trying to fetch from URL (null) - The file couldn’t be opened.

仅当 URL 字符串包含字符“|”时才会出现这些错误

id=1 (pageids=1) 的文章的 URL 是:

https://en.wikipedia.org/w/api.php?action=query&format=json&pageids=1&prop=extracts&exintro&explaintext

上面的 URL 不包含字符“I”,所以它工作得很好。

在维基百科 API 中,您可以通过用“|”分隔它们的 id 来请求多篇文章性格

ids=1,2 和 3 (pageids=1|2|3) 的文章的 URL 是:

https://en.wikipedia.org/w/api.php?action=query&format=json&pageids=1|2|3&prop=extracts&exintro&explaintext

上面的网址包含“|”性格和一切都失败了。

我使用我在另一篇文章中找到的这个片段来捕捉错误:

NSError *error = NULL;
NSStringEncoding actualEncoding;

NSString *string = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&actualEncoding error:&error];
if(string)
{
    NSLog( @"hey,I actually got a result of %@",string);

    if(actualEncoding != NSUTF8StringEncoding)
    {
        NSLog( @"and look at that,the actual encoding wasn't NSUTF8StringEncoding");
    }
} else {
    NSLog( @"error when trying to fetch from URL %@ - %@",[url absoluteString],[error localizedDescription]);
}

如果你仔细阅读代码,url.absoluteString 会在有“|”时返回 null里面的字符。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)