尝试使用 NSURL 访问 Google 翻译时出错
如果我尝试此代码:
NSError *err = nil;
NSString *resp = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=abdicate&langpair=en|es"]
encoding:NSASCIIStringEncoding
error:&err];
Err 包含以下消息: Error Domain=NSCocoaErrorDomain Code=258 UserInfo=0x1001166d0 “文件名无效。”
有人知道这是什么意思吗?如果我在浏览器上尝试该网址,效果很好。
If I try this code:
NSError *err = nil;
NSString *resp = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=abdicate&langpair=en|es"]
encoding:NSASCIIStringEncoding
error:&err];
Err contains the following message: Error Domain=NSCocoaErrorDomain Code=258 UserInfo=0x1001166d0 "The file name is invalid."
Anybody knows what this means? If I try the url on a browser, it works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(使用
NSData
)您只需将1.0
替换为1%2E0
并将|
替换为% 7C。句点可能被解释为带有扩展名的文件,因此出现 258 错误 (来自基础常量参考):
试试这个:
给出输出:
这与您通过导航到网址 https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=abdicate&langpair=en|es 在浏览器中。
<罢工>
尝试使用 NSData,然后转换为字符串,如下所示:
这是上面使用的 urlencode 方法:
这整个部分是不必要的,只需替换 1.0 和 | 即可。而不是对整个 url 进行编码。
(Using
NSData
) you can just replace the1.0
with1%2E0
and the|
with%7C
. It is possible that with the period is being interpreted as a file with an extension, thus the 258 error (From Foundation Constants Reference):Try this:
Gives the output:
Which is the same as what you get by navigating to the url https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=abdicate&langpair=en|es in a browser.
Try using NSData, then converting to a string, like so:
This is the urlencode method used above:
This whole part was unnecessary, just need to replace the 1.0 and | instead of encoding the entire url.