ios - 来自 HTTPS URLSession.dataTask 的 0 字节数据

标签 ios swift https tls1.2 nsurlsessiondatatask

我正在尝试从 URL 检索一些 JSON 数据,但它每次都在数据中返回 0 字节。我检查了几个不同的 SO 帖子,但似乎没有一个与我遇到的确切问题相符,因为我从中获取数据的服务器确实使用带有 TLS 1.2 的 HTTPS。所以我不认为有任何东西需要添加到我的 info.plist 中,尽管我可能是错的。

这是我的代码:

var tournaments: [Tournament] = []

/// Base API URL
let baseURL: String = "https://www.burningmeter.com/tournaments.json?page=1"

// ...

func retrieveAPIData() {
    // Build the API string
    var request: URLRequest = URLRequest(url: URL(string: baseURL)!)
    request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
    request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "GET"

    // Request the data
    let session: URLSession = URLSession.shared
    let task = session.dataTask(with: request) { (data, response, error) in

        // Did we get an error?
        guard error == nil else {
            print(error!)
            return
        }

        guard let json = data else {
            print("No data")
            return
        }

        guard json.count == 0 else {
            print("Zero bytes of data")
            return
        }

        let jsonDecoder = JSONDecoder()
        let tournaments = try! jsonDecoder.decode(TournamentPage.self, from: json)

        // We got our values, let's go
        self.tournaments = tournaments.tournaments
    }
    task.resume()
}


锦标赛结构:

struct Tournament : Codable {
    let id: Int
    let name: String
    let game_id: Int
    let game_iteration_id: Int
    let state: Int
    let starts_at: String
    let creator_id: Int
    let stream_url: String
    let entrant_count: Int
    let prereg_count: Int
    let path: String
}

struct TournamentPage : Codable {
    let page: Int?
    let results_per_page: String
    let tournament_count: Int
    let tournaments: [Tournament]
}

最佳答案

您的 guard 语句有错误。您在 json.count == 0 中编写了 == 而不是 !=。它应该像下面的代码一样:

guard json.count != 0 else {
    print("Zero bytes of data")
    return
}

使用以下代码在 Playground 中进行测试(已删除编码器):

let baseURL: String = "https://www.burningmeter.com/tournaments.json?page=1"


func retrieveAPIData() {
    var request: URLRequest = URLRequest(url: URL(string: baseURL)!)
    request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
    request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "GET"

    // Request the data
    let session: URLSession = URLSession.shared
    let task = session.dataTask(with: request) { (data, response, error) in
        print(data!.count)
        print(response)
        // Did we get an error?
        guard error == nil else {
            print(error!)
            return
        }

        guard let json = data else {
            print("No data")
            return
        }

        guard json.count != 0 else {
            print("Zero bytes of data")
            return
        }
        print(String(decoding: json, as: UTF8.self))
    }
    task.resume()
}

retrieveAPIData()

打印结果:

8031
Optional(<NSHTTPURLResponse: 0x7fbce1650fc0> { URL: https://www.burningmeter.com/tournaments.json?page=1 } { Status Code: 200, Headers {
    "Access-Control-Allow-Origin" =     (
        "*"
    );
    "Cache-Control" =     (
        "max-age=0, private, must-revalidate"
    );
    Connection =     (
        "keep-alive"
    );
    "Content-Type" =     (
        "application/json; charset=utf-8"
    );
    Date =     (
        "Thu, 08 Nov 2018 15:40:40 GMT"
    );
    Etag =     (
        "W/\"5d5a17c8ff6f705d6bb56c6ed8b6a099\""
    );
    Server =     (
        Cowboy
    );
    "Strict-Transport-Security" =     (
        "max-age=31536000"
    );
    "Transfer-Encoding" =     (
        Identity
    );
    Via =     (
        "1.1 vegur"
    );
    "X-Content-Type-Options" =     (
        nosniff
    );
    "X-Frame-Options" =     (
        SAMEORIGIN
    );
    "X-Request-Id" =     (
        "0d7f8ae9-92d2-41d8-8678-6c9b2bdb5e3b"
    );
    "X-Runtime" =     (
        "0.044064"
    );
    "X-Xss-Protection" =     (
        "1; mode=block"
    );
} })


JSON 结果:

{
    "page": "1",
    "results_per_page": "50",
    "tournament_count": 33,
    "tournaments": [{
            "id": 1872,
            "name": "Pinup Biweekly - Nov 1",
            "game_id": 90,
            "game_iteration_id": 133,
            "state": 30,
            "starts_at": "2018-11-02T00:00:00.000Z",
            "creator_id": 960,
            "stream_url": null,
            "entrant_count": 6,
            "prereg_count": 0,
            "path": "/t/1872/pinup-biweekly-nov-1"
        }, {
            "id": 1874,
            "name": "IBTY #45",
            "game_id": 21,
            "game_iteration_id": 38,
            "state": 30,
            "starts_at": "2018-11-02T22:03:00.000Z",
            "creator_id": 185,
            "stream_url": null,
            "entrant_count": 11,
            "prereg_count": 0,
            "path": "/t/1874/ibty-45"
        }, {
            "id": 1875,
            "name": "SFV Weekly 11/2/18",
            "game_id": 6,
            "game_iteration_id": 14,
            "state": 30,
            "starts_at": "2018-11-03T00:55:00.000Z",
            "creator_id": 957,
            "stream_url": null,
            "entrant_count": 9,
            "prereg_count": 0,
            "path": "/t/1875/sfv-weekly-11-2-18"
        }, {
            "id": 1876,
            "name": "Weekly SC6 11/2/18",
            "game_id": 106,
            "game_iteration_id": 149,
            "state": 20,
            "starts_at": "2018-11-03T01:04:00.000Z",
            "creator_id": 957,
            "stream_url": null,
            "entrant_count": 6,
            "prereg_count": 0,
            "path": "/t/1876/weekly-sc6-11-2-18"
        }, {
            "id": 1879,
            "name": "UNIST - WS 11/3",
            "game_id": 12,
            "game_iteration_id": 23,
            "state": 30,
            "starts_at": "2018-11-03T18:30:00.000Z",
            "creator_id": 949,
            "stream_url": null,
            "entrant_count": 8,
            "prereg_count": 0,
            "path": "/t/1879/unist-ws-11-3"
        }, {
            "id": 1881,
            "name": "DBFZ - WS 11/3",
            "game_id": 86,
            "game_iteration_id": 129,
            "state": 30,
            "starts_at": "2018-11-03T18:30:00.000Z",
            "creator_id": 949,
            "stream_url": null,
            "entrant_count": 8,
            "prereg_count": 0,
            "path": "/t/1881/dbfz-ws-11-3"
        }, {
            "id": 1882,
            "name": "SFV - WS 11/3",
            "game_id": 6,
            "game_iteration_id": 14,
            // more
        }
        // More output here
}

接收到的长度为8031

关于ios - 来自 HTTPS URLSession.dataTask 的 0 字节数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210721/

相关文章:

ios - 使用 UIApplicationDidEnterForeground 快速更新 tableview

ios - 展开可选值 (UIButton) 时意外发现 nil

python - 通过 HTTPS 运行 Eve

amazon-web-services - AWS,在 Windows EC2 实例上启用 HTTPS 的步骤?

ios - UICollectionViewCell 的后台线程快照 = WebCore 崩溃

ios - 了解核心视频 CVPixelBufferPool 和 CVOpenGLESTextureCache 语义

objective-c - Objective-C : How can I render Chinese fonts in OpenGL?

ios - 了解 ARKit 中的坐标空间

ios - 如何在 SKView 之上添加 SCNView

python - 我设置了 https,但数据仍然响应没有证书的客户端