gpt4 book ai didi

swift - iOS9 - NSURLSession 无法解析响应,但 AlamoFire 可以工作

转载 作者:行者123 更新时间:2023-11-30 13:07:35 25 4
gpt4 key购买 nike

我正在尝试从 AlamoFire 转移到 NSURLSessions 并尝试向返回 的端点发出 GET 请求JSON(字典数组)。

下面是来自 Alamofire 代码的 header ,该代码成功:

GET /Path/?lastrequest=2006-08-25T03:25:21Z HTTP/1.1
Host one.domain.com
Cookie connect.sid=s%3A8gUYZmxHWn9PJ95ZJF9P6FGrdrcPIueG.KcXee%2FT8VI1SeAaVvOwcHdOuCLydPvU%2BLiqlZHu2QfI
x-token $2a$10$Vif5iFua8/EdITuaibyO.ANoGm8EuNORBLjXVaxwSJ.a.1472443117
Accept */*
User-Agent MyApp/com.MyApp.test (1; OS Version 9.3 (Build 13E230))
Accept-Language en-US;q=1.0
Accept-Encoding gzip;q=1.0,compress;q=0.5
Connection keep-alive

AlamoFire 成功响应信息:

Size    
Request Header 475 bytes
Response Header 173 bytes
Request -
Response 87.66 KB (89762 bytes)
Total 88.29 KB (90410 bytes)
Request Compression -
Response Compression -

Response Code 200 OK
Protocol HTTP/1.1
Method GET
Kept Alive Yes
Content-Type application/json; charset=utf-8
Client Address /127.0.0.1

现在使用 NSURLSession 的 header 失败:

GET /Path/?lastrequest=2006-08-25T03:25:21Z HTTP/1.1
Host one.domain.com
Accept */*
Connection keep-alive
x-token $2a$10$Vif5iFua8/EdITuaibyO.ANoGm8EuNORBLjXVaxwSJ.a.1472443117
Cookie connect.sid=s%3ATRSrme3Q4yLchJu-3KanYLi8lKJgITbh.xO0uUEIFsWQ7McIyA36%2BgJi7aPWb9MYxSEn9nYKzUA8
Accept-Language en-US;q=1.0
Content-Length 2
Accept-Encoding gzip;q=1.0,compress;q=0.5
User-Agent NetworkLayer/1 CFNetwork/758.3.15 Darwin/15.6.0

NSURLSession 失败响应信息:

Size    
Request Header 476 bytes
Response Header 173 bytes
Request -
Response 12.73 KB (13032 bytes)
Total 13.36 KB (13681 bytes)
Request Compression -
Response Compression -

我确实在 header 中看到了 AlamoFire 中没有的额外字段,但我不知道如何将其关闭。

NRURLSession 还进行 3-4 个并发调用。

NSURLSession 失败响应:

Response:  Failed to parse data (org.json.JSONException: A JSONObject text must begin with '{' at character 1) 

Failure Client closed connection before receiving entire response
Response Code 200 OK
Protocol HTTP/1.1
Method GET
Kept Alive No
Content-Type application/json; charset=utf-8
Client Address /127.0.0.1

这是我在 NSURLSession 中配置的 header:

 var headers: [String: String]? {
return ["x-token" : token!,
"Accept-Encoding": "gzip;q=1.0,compress;q=0.5",
"Accept-Language": "en-US;q=1.0"]

我做错了什么?

最佳答案

Content-Length = 2 不断出现在 header 中的原因是因为我为字典发送了一个空字符串。当我发送一个 nil 时,它就像一个魅力。

关于swift - iOS9 - NSURLSession 无法解析响应,但 AlamoFire 可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com