Alamofire失败,出现SSL错误代码:-1200

时间:2018-03-26 09:22:59

标签: ios swift alamofire

我正在连接到自签名URL,因此我已配置为此URL禁用SSL证书评估。我正在使用Alamofire进行网络操作。

static let sessionManager: SessionManager = {
    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = NetworkConfiguration.sessionTimeoutInterval
    configuration.httpAdditionalHeaders = [
        "Accept": "application/json",
        "Content-Type": "application/json"
    ]
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "xyz.com:12345" : .disableEvaluation
    ]
    let trustPolicyManager = ServerTrustPolicyManager(policies: serverTrustPolicies)
    return Alamofire.SessionManager(configuration: configuration, serverTrustPolicyManager: trustPolicyManager)
}()

我还在info.plist文件中禁用了ATS:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

这是我连接到API服务的方式:

        WebService.sessionManager.request(url, method: .get, parameters: params, encoding: resource.method.encodingFormat(), headers: headers)
            .validate(statusCode: 200..<300)
            .validate(contentType: ["application/json"])
            .responseJSON { response in

                let requestAPI = response.request?.url?.absoluteString
                print("**********network Manager request: \(requestAPI ?? "request not found") ")

                switch response.result {
                case .success:
                    if let json = response.data {
                        print("############Network Manager response: \(String(data: json, encoding: String.Encoding.utf8) ?? "Response Data could not be printed")")
                        completion(json, nil)
                    } else {
                    }

                case .failure(let error):
                    let errorFailureCode = (error as NSError).code
                    print("Network manager failure with reason: \(error.localizedDescription)")
                    completion(nil, error)
                }

        }

尽管如此,我收到了错误:

  

NSURLSession / NSURLConnection HTTP加载失败   (kCFStreamErrorDomainSSL,-9800)       任务&lt; 68BD48CA-81E0-427C-B37F-50091A66EC7C&gt;。&lt; 1&gt;。 HTTP加载失败(错误代码:-1200 [3:-9800])       Alamofire错误 - 发生SSL错误,无法与服务器建立安全连接。

1 个答案:

答案 0 :(得分:0)

尝试从主机字符串中删除端口。

let serverTrustPolicies: [String: ServerTrustPolicy] = [
    "xyz.com" : .disableEvaluation
]