撥打的電話這是我開始我更新的iOS 11(15A5304i)和Xcode的9(9M137d)之後的最新測試版的問題。狀態碼400:NSHTTPURLResponse使用URLSession.shared.dataTask
當我恢復到以前的測試,它消失了。
進行任何GET HTTPS調用從服務器400響應網站的結果。
networkError(FoodAPI.NetworkError.responseStatusError(status: 400, message: "<NSHTTPURLResponse: 0x1c4239700> { URL: https://stackoverflow.com/ } { status code: 400, headers {\n Date = \"Thu, 22 Jun 2017 17:41:03 GMT\";\n} }"))
這就是我正在做的通話
func callTask(_ request: URLRequest, completion: @escaping (ResultType<Data, NetworkError>) -> Void) {
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let response = response as? HTTPURLResponse else {
completion(.failure(.responseInvalidFormat))
return
}
if response.statusCode >= 400 {
let error = response.description
completion(.failure(.responseStatusError(status: response.statusCode, message: error)))
return
}
guard let data = data else {
completion(.failure(.responseNoDataError))
return
}
completion(.success(data))
}
task.resume()
}
,這是該請求是如何產生的:
private func generateRequest(urlString: String,
method: HttpMethod = .get,
contentType: ContentType = .json,
headers: [String: String] = ["":""],
body: [String: Any]? = nil) throws -> URLRequest {
guard let url = URL(string: urlString) else {
throw NetworkError.requestGenerationFail(url: urlString, httpMethod: method)
}
var request = URLRequest(url: url)
request.httpMethod = method.rawValue
request.setValue("***** by Reza: info: [email protected]", forHTTPHeaderField: "User-Agent")
request.allHTTPHeaderFields = headers
if contentType == .urlEncoded && headers["content-type"] ?? "" != ContentType.urlEncoded.rawValue {
request.addValue(ContentType.urlEncoded.rawValue, forHTTPHeaderField: "content-type")
}
if let body = body {
request.httpBody = try self.generateBody(body: body, contentType: contentType)
}
return request
}
同樣,這個工作正常在以前的iOS測試版11 ,但是新的HTTP響應會給Web服務器帶來400響應,幾乎沒有數據。
值得一提的是,我第一次作出這是對https://api.yelp.com/託管Yelp的端點API的調用。此連接成功,沒有任何問題。到其他主機(https://stackoverflow.com,https://www.yelp.com)任何進一步的調用回來了400
當我運行使用舊的測試版在模擬器中的代碼,它工作正常,當我部署到我的iPhone 6是採用了全新的iOS它400個來自服務器。
我沒有對我自己的服務器進行測試,所以我可以查看日誌,但是沿途的一些事情正在破壞並導致服務器返回一個錯誤的請求響應。這些相同的調用使用curl,郵差,瀏覽器和以前的iOS版本都可以很好地工作。
我希望誰也面臨着同樣的問題可以提供一些線索的人。
您是否使用兩個測試版比較了原始請求(例如查爾斯,或僅僅是傾銷數據)?這可能會告訴你什麼是問題(param enconding?etc) – nathan
@nts所有的通信都是加密的,所以我在Charles上看到的兩個beta都是不可讀的。有趣的是,調用http端點成功。 –
自定義加密或只是HTTPS? – nathan