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

问题描述:

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

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() 

的NSHTTPURLResponse对象不是应该包含数据。它仅包含元数据(标题等)。正文数据在数据对象中。

+0

是的,对我做出了非常愚蠢的错误:)谢谢+1 –