从网站获取JSON时出现错误

时间:2018-07-19 17:03:32

标签: json swift http cookies header

从网站获取JSON时出现错误。我猜该网站阻止了我获取数据。我该如何解决?

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=SH600004&begin=1531842004299&period=day&type=before&count=-142&indicator=kline,ma,macd,kdj,boll,rsi,wr,bias,cci,psy")
        var urlRequest = URLRequest(url: url)

        let manager = Alamofire.SessionManager.default
        manager.request(url).responseJSON { (dataResponse) in
            do {
                if dataResponse.data != nil {
                    let json =  try JSON(data: dataResponse.data!)
                    print(json.description)
                }
            } catch {
                print("Error: trying to convert data to JSON2")
                print(error)
                completionHandler(nil, error)
            }
        }
    }
}

但是下面出现错误:

{
  "error_code" : "400016",
  "error_uri" : "\/v5\/stock\/chart\/kline.json",
  "error_description" : "Encountered an error, please refresh the page or re-login and try again"
}

2 个答案:

答案 0 :(得分:0)

当我将URL复制到浏览器中时,我得到了很多数据-不是您所说的错误。代码现在可以工作了吗?也许是服务器错误...

答案 1 :(得分:0)

我认为您错过了请求中的身份验证部分,因为收到的HTTP错误是403,这是禁止的