【问题标题】:How to call HTTPS URL with certificate Authentication如何使用证书身份验证调用 HTTPS URL
【发布时间】:2016-06-28 05:50:57
【问题描述】:

我正在使用证书和身份验证实现一个第三方 API。我已将证书放在我的资源文件夹中,我也有密码。对于上述情况,我认为我实现了 NSURLSession 和委托方法“didrecieve challenge”几乎可以正常工作。但作为回应,我只得到标题部分而不是正文请帮助我犯了什么错误。来自服务器的响应应该是 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()

【问题讨论】:

    标签: ios swift authentication nsurlsession


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-05
      • 1970-01-01
      • 2023-03-29
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      相关资源
      最近更新 更多