2016-06-28 101 views
1

我正在致力於使用證書和身份驗證來實現一個第三方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() 

回答

1

的NSHTTPURLResponse對象不是應該包含數據。它僅包含元數據(標題等)。正文數據在數據對象中。

+0

是的,對我做出了非常愚蠢的錯誤:)謝謝+1 –