簡體   English   中英

Swift URLSession.shared.dataTask GET Request -1001返回超時

[英]Swift URLSession.shared.dataTask GET Request -1001 returns timeout

將POST請求發送到我們的NGINX服務器可與URLRequestURLSession.shared.dataTask一起很好地工作。 我可以登錄我的應用程序,但是當我嘗試GET請求時,我的服務器沒有該請求到達他的日志。 最后,我收到超時錯誤。 重要提示,我正在使用https。 我也嘗試將http用於GET請求。 在我的NGINX服務器上,我僅將TLS設置為1.2(我不是系統專家,我在nginx cfg文件中做到了這一點。

錯誤域= NSURLErrorDomain代碼= -1001“在Anforderung區域中。 UserInfo = {NSUnderlyingError = 0x60400005abe0 {Error Domain = kCFErrorDomainCFNetwork Code = -1001“(null)” UserInfo = {_ kCFStreamErrorCodeKey = -2102,_kCFStreamErrorDomainKey = 4}},NSErrorFailingURLStringKey = https:// myurl ,NSErrorFailingURLKey = https:// myurl , _kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -2102,NSLocalizedDescription =位於安福德格倫(Zeitüberschreitung)附近。}

我確信URLRequestURLSession代碼是正確的,因為針對localhost和我們的開發環境,我沒有任何這些問題。

那就是我的代碼來創建我的URLRequest

private func buildUrlRequest(jsonObject:[String: Any], connectionType:ConnectionTypes, url:String) -> NSMutableURLRequest? {
    let jsonDataSerialized:Data
    do {
        jsonDataSerialized = try JSONSerialization.data(withJSONObject: jsonObject)
    } catch {
        return nil
    }
    var request:NSMutableURLRequest
    if nextData {
        request = URLRequest(url: URL(string: self.nextRequestPage)!) as! NSMutableURLRequest
    } else {
        let tString = self.mBaseUrl + url
        if let encoded = tString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
            var finalurl = URL(string: encoded)
        {
            if connectionType == .GET {
                var tString:String
                tString=finalurl.absoluteString
                tString = tString.replacingOccurrences(of: "https", with: "http")
                finalurl = URL(string:tString)!
            }
            request = URLRequest(url: finalurl) as! NSMutableURLRequest
        } else {
            return nil
        }
    }
    request.httpMethod = connectionType.rawValue // i am using enum here
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("no-cache)", forHTTPHeaderField: "Cache-Control")
    request.httpBody = jsonDataSerialized
    if mUser != nil && mUser.getSessionId() != nil {
        request.addValue("Token " + mUser.getSessionId()!, forHTTPHeaderField: "Authorization")
    }
    request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
    request.timeoutInterval = 30.0
    return request
}

這就是我創建任務的方式...在我使用task.resume()的方法體之后

let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in .... (and so on)

我花了多個小時來解決這個問題...但是我不知道。 我不確定問題是服務器配置還是Swift代碼...

主要問題是jsonObject:[String: Any] 我將其更改為jsonObject:[String: Any]?

在我的代碼中,無論是POST GET PUT還是其他任何事情,我都為每個請求創建了正文。 在GET請求中沒有http-body,我再也沒有問題了,沒有超時! 我正確地收到了預期的數據。

我還發現,僅在我的情況下,不需要設置nginx或讓sll_protocols加密為TLS 1.2。

我希望如果有人遇到此問題,他們會找到這篇文章:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM