2016-08-26 34 views
0

我試圖移開從AlamoFireNSURLSessions,並試圖使GET請求到返回JSON(一個字典數組)的端點。iOS9 - NSURLSession無法解析響應,但AlamoFire工作

下面是從Alamofire代碼,成功頭:

AlamoFireSuccessful響應信息:

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失敗頭

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 

這裏是header,我在NSURLSession配置:

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"] 

我在做什麼錯?

+0

它是你的端點還是第三方?服務器可能會阻止你基於,比如用戶代理嗎? –

回答

0

Content-Length = 2不斷顯示在標題中的原因是我爲字典發送了一個空字符串。當我發送一個零時,它的工作就像一個魅力。