简体   繁体   English

XCode快速HTTPS:字符串(数据:数据!,编码:NSUTF8StringEncoding)返回nil

[英]XCode swift HTTPS: String(data: data!, encoding: NSUTF8StringEncoding ) return nil

I am retrieving a HTML content from an HTTPS page, but for "short" content I do get it correctly. 我正在从HTTPS页面检索HTML内容,但是对于“短”内容,我确实可以正确获取它。 For "long" content I get a nil answer. 对于“长篇”内容,我得到的答案是零。

I don't know if content length has something to do with it. 我不知道内容长度是否与它有关。

My page either return a report in HTML (a small table with a couple of rows) or an "error" message if report is not found. 我的页面要么返回HTML报告(一个带有几行的小表),要么返回“错误”消息(如果未找到报告)。

When the error message is returned, it "works well" --> I got the HTML response, but when it founds the report, I got a nil answer. 返回错误消息后,它“运行良好”->我得到了HTML响应,但是当发现报告时,我得到了零答案。

The report is working fine in HTML browser. 该报告在HTML浏览器中运行良好。 I checked the apache logs and the answer content has the correct length, but Xcode got a nil answer. 我检查了apache日志,答案内容的长度正确,但是Xcode的答案为零。

I already checked dozen of "HTTPS nil request" questions found on Internet. 我已经检查了Internet上出现的许多“ HTTPS无请求”问题。 HTTPS is not the problem. HTTPS不是问题。 Code is working without need of having to put "Allow Arbitrary Loads" to true or adding "Exception Domains". 代码可以正常工作,而无需将“允许任意加载”置为true或添加“异常域”。 Enabling these option do not solve the problem. 启用这些选项不能解决问题。

I have no idea what could cause this nil request while server is sending it. 我不知道是什么原因导致服务器发送此nil请求。

let curUsuario=curTuc.nombre
let urlString = "https://xxxxxx.yyyyy.com/PatientLabDetails5?usuario=\(curUsuario)&pin=\(curnoPin)"
let url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
                        (data, response, error) in

 print("response: \(response)")
 print("Data: \(data?.length)")

....
};
task.resume()

Debug: 调试:

response: Optional(<NSHTTPURLResponse: 0x7aea3f30> { URL: https://xxxx.yyyy.com/PatientLabDetails5?usuario=25808&pin=4884938 } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Type" = "text/html;charset=ISO-8859-1";
    Date = "Tue, 01 Nov 2016 14:07:29 GMT";
    "Keep-Alive" = "timeout=15, max=100";
    Server = Apache;
    "Set-Cookie" = "JSESSIONID=2A38EC49C93927A34AA1C9ED16B5F997; Path=/; Secure; HttpOnly";
    "Transfer-Encoding" = Identity;
    "X-Content-Type-Options" = nosniff;
    "X-Frame-Options" = SAMEORIGIN;
    "X-XSS-Protection" = "1; mode=block";
} })

Data: 6015

But when I try to get the data, I got a nil result.. 但是当我尝试获取数据时,结果为零。

print(String(data: data!, encoding: NSUTF8StringEncoding ))

I tested it on 3 servers with apparently same apache settings, and exactly same web server code. 我在3个服务器上用相同的apache设置和完全相同的Web服务器代码对其进行了测试。 On one it does work well, on the other 2 not. 一方面效果很好,另一方面则效果不佳。 As usual, the one working is the one I don't need to use. 与往常一样,一种工作就是我不需要使用的工作。

I checked the SSL certificate installation (just in case) on www.htbridge.com/ssl and got a A+ grade. 我在www.htbridge.com/ssl上检查了SSL证书的安装(以防万一),并获得A +等级。

in my code usually make a fall back: 在我的代码中通常回退:

 var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
 if (strData == nil){
    //roll back to ASCII ...
        strData = NSString(data: data, encoding: NSASCIIStringEncoding)
    }

I finally got it! 我终于明白了!

Thanks to comment from Alejandro Avilàn on String(data: data, encoding: NSUTF8StringEncoding) return nil question, I replaced NSUTF8StringEncoding by NSASCIIStringEncoding and my problem is now solved. 感谢AlejandroAvilàn对String(数据:数据,编码:NSUTF8StringEncoding)返回nil问题的评论,我将NSUTF8StringEncoding替换为NSASCIIStringEncoding ,现在我的问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迅速地在println(NSString(data:data,encoding:NSUTF8StringEncoding))中表示这些(响应,数据,错误)是什么意思? - What does these (response, data, error) in println(NSString(data: data, encoding: NSUTF8StringEncoding)) means in swift? 带有openssl加密数据的NSString initWithBytes使用NSUTF8StringEncoding返回nil - NSString initWithBytes with openssl encrypted data returns nil with NSUTF8StringEncoding 将数据Iphone发送到PHP NSUTF8StringEncoding - Sending Data Iphone to PHP NSUTF8StringEncoding NSUTF8StringEncoding返回nil NSString - NSUTF8StringEncoding returns nil NSString 在Swift中stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)返回Optional - stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) returns Optional in Swift NSUTF8StringEncoding的目的 - NSUTF8StringEncoding purpose NSUTF8StringEncoding结果编码的字符串,带有“可选”字符串 - NSUTF8StringEncoding result encoded string with 'Optional' string NSUTF8StringEncoding 和 NSUTF16StringEncoding 有什么区别? - What are different between NSUTF8StringEncoding and NSUTF16StringEncoding? IOS7中的NSUTF8StringEncoding问题 - NSUTF8StringEncoding issue in IOS7 字符串通过使用编码添加百分号转义:NSUTF8StringEncoding不转换某些特殊字符 - stringbyaddingpercentescapesusingencoding:NSUTF8StringEncoding not converting some special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM