[英]NSURLSession does not returns data for http/https
I need to get the data from the url it works for other url like udemy but returns nil for url="http://www.google.com/finance/converter?a=1&from=USD&to=INR" how to solve this issue. 我需要从适用于其他网址(例如udemy)的网址中获取数据,但对于网址=“ http://www.google.com/finance/converter?a=1&from=USD&to=INR”返回nil如何解决此问题。 i have added app transport security too.. 我也增加了应用程序传输安全性。
let url = NSURL(string: "https://www.udemy.com/the-complete-ios-9-developer-course/")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let url_content = data{
let webcontent = NSString(data: url_content, encoding: NSUTF8StringEncoding)
print(webcontent)
}
}
task.resume()
You can check the "error" variable to see if you have an error returned by the call. 您可以检查“错误”变量,以查看调用是否返回了错误。 Most probably you have an error accessing that url. 很可能您访问该网址时出错。
Note that I tried the code and I could get data. 请注意,我尝试了代码,然后可以获取数据。
This is a text encoding problem. 这是一个文本编码问题。 The webpage at http://www.google.com/finance/converter?a=1&from=USD&to=INR is not encoded with UTF-8
but with ISO-8859-1
. http://www.google.com/finance/converter?a=1&from=USD&to=INR上的网页不是使用UTF-8
编码的,而是使用ISO-8859-1
。
In this case, you have to use NSISOLatin1StringEncoding
instead of NSUTF8StringEncoding
for NSString: 在这种情况下,您必须对NSString使用NSISOLatin1StringEncoding
而不是NSUTF8StringEncoding
:
let webcontent = NSString(data: url_content, encoding: NSISOLatin1StringEncoding)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.