[英]How to bypass HTTPS server request using swift
我想使用URLSession繞過HTTPS服務器請求。 我有一個客觀的C代碼,並且運行良好。 但是,當我將此代碼轉換為swift時,它不起作用。 請檢查並幫助我。 我在代碼中做錯了什么?
// Objective C Code
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
// Swift Code
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let aTrust = challenge.protectionSpace.serverTrust {
completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: aTrust))
}
}
除了是一個壞主意之外(如果此代碼最終出現在運輸應用程序中,您將遇到一個非常嚴重的安全漏洞),這也將破壞您通過代理提出請求,破壞身份驗證等的能力。請不要做這個。
您可以從Let's Encrypt獲得免費的證書,因此,除非有涉及未連接到Internet的自定義硬件的事情,即使有,禁用驗證仍然是沒有理由使用自簽名證書。甚至不是遠程的正確方法。
只需刪除代碼並安裝免費證書即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.