简体   繁体   English

NSURLSession不返回http / https的数据

[英]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.. 我也增加了应用程序传输安全性。

screen 屏幕

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.

相关问题 Swift / https:NSURLSession / NSURLConnection HTTP加载失败 - Swift/https: NSURLSession/NSURLConnection HTTP load failed NSURLSession HTTP基本身份验证委托不起作用 - NSURLSession HTTP basic auth delegate does not work iOS 9中的HTTPS请求:NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802) - HTTPS request in iOS 9 : NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) 用于 HTTP 数据任务 (NSURLSessionDataTask) 的 NSURLSession 是否在后台线程中运行,或者我们必须提供队列? - Does NSURLSession for HTTP data task (NSURLSessionDataTask) runs in background thread or we will have to provide the queue? NSURLSession 和 HTTP - NSURLSession and HTTP 如果NSURLSession#dataTaskWithRequest返回nil是什么意思? - What does it mean if NSURLSession#dataTaskWithRequest returns nil? iOS NSURLSession是否支持HTTP 100-continue? - Does iOS NSURLSession support HTTP 100-continue? NSURLSession有时会返回数据,但有时不会返回iOS 9 - Swift 2.0 - NSURLSession sometimes returns data but sometimes don't iOS 9 - Swift 2.0 使用C#与NSUrlSession进行HTTP POST多部分表单数据 - HTTP POST multipart form-data with NSUrlSession using c# NSURLSession downloadTaskWithURL返回nill - NSURLSession downloadTaskWithURL returns nill
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM