在iOS中访问NSURLRequest中的“日期”HTTP标头

时间:2017-02-20 11:05:55

标签: ios http nsurlrequest

有没有办法在iOS中的NSURLRequest中访问'Date'HTTP标头,例如在授权过程中使用它? (例如http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html

1 个答案:

答案 0 :(得分:0)

试试此代码

    let config = URLSessionConfiguration.default 
    let session = URLSession(configuration: config) 
    let url = URL(string:"https://johnsmith.s3.amazonaws.com")!
    let request = NSMutableURLRequest(url: url)
    request.timeoutInterval = 60
    request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "GET"
    request.addValue(" "AWS" + " " + AWSAccessKeyId + ":" + Signature", forHTTPHeaderField: "Authorization")
    let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
        //        (data, response, error) in
        if error != nil {
            print(error!.localizedDescription)
        } else {
            do {
                let json = try JSONSerialization.jsonObject(with: data! as Data, options: .allowFragments)
                print("json data")   

            } catch {
                print("error in JSONSerialization")
            }
        }
    }
    task.resume()