如何使用证书身份验证调用HTTPS URL

时间:2016-06-28 05:50:57

标签: ios swift authentication nsurlsession

我正在开发使用证书和身份验证实现一个第三方API。我已将证书放在我的资源文件夹中,我也有密码。对于以上认为我实施NSURLSession和委托方法“didrecieve挑战”几乎工作正常。但作为回应我只得到标题部分而不是身体请帮助我做了什么错误。服务器的响应应该是XML格式,但只有以下部分:

status code: 200, headers {
    Connection = close;
    "Content-Length" = 23113;
    "Content-Type" = "text/html; charset=ISO-8859-1";
    Date = "Tue, 28 Jun 2016 05:26:42 GMT";
    Server = "Apache/2.2.15 (CentOS)";
} }) 

代码:

let xmlString = "<?xml version='1.0' encoding='ISO-8859-1'?><TICKETANYWHERE><COUPON VER='1.0'><TEMPLATELIST /></COUPON></TICKETANYWHERE>"
        let xmlData = xmlString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

        let request = NSMutableURLRequest(URL: NSURL(string: "My URL")!)
        request.HTTPMethod = "POST"
        request.HTTPBody = xmlData
        request.addValue("text/html; charset=ISO-8859-1", forHTTPHeaderField: "Content-Type")
        request.setValue("Apache/2.2.15 (CentOS)", forHTTPHeaderField: "Server")
        request.addValue("23113", forHTTPHeaderField: "Content-Length")

        struct SessionProperties {
            static let identifier : String! = "url_session_background_download"
        }


        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        request.timeoutInterval = 20.0 //(number as! NSTimeInterval)

        let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
        let task = session.dataTaskWithRequest(request) { data, response, error in
            **print(response)** // Got Only Header Part Not Body
        }

        task.resume()

1 个答案:

答案 0 :(得分:1)

NSHTTPURLResponse对象不是假设来包含数据。它仅包含元数据(标题等)。正文数据位于数据对象中。