Dipen Chudasama
Dipen Chudasama

Reputation: 3093

How to call HTTPS URL with certificate Authentication

I was working on Implementing one third party API with certificate and Authentication. I have put certificate in my resource folder, I also have password for that. For the above think I implement NSURLSession and delegate method "didrecieve challenge" almost working fine. But In response I Only get Header parts not body Please help me what mistack I made. The response from server should be XML format but Got only below parts :

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)";
} }) 

Code:

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

Upvotes: 1

Views: 168

Answers (1)

dgatwood
dgatwood

Reputation: 10417

An NSHTTPURLResponse object is not supposed to contain data. It contains only the metadata (headers, etc.). The body data is in the data object.

Upvotes: 1

Related Questions