- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 iOS 应用程序中,我使用默认配置创建一个 URLSession:
// Part #1
let urlSession = URLSession(configuration: .default)
用户登录,设置 cookie,并且所有 API 请求都授权并且工作正常。现在,使用相同的 cookie,我想创建一个后台 URLSession,用于从同一服务器下载大文件。
// Part #2
lazy var downloadsSession: URLSession = {
let configuration = URLSessionConfiguration.background(withIdentifier:"x.bgSession")
// Use cookies from shared storage
configuration.httpCookieStorage = HTTPCookieStorage.shared
return URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}()
下载请求失败,服务器返回未授权。使用mitmproxy当我拦截此请求时,我注意到对于 downloadsSession,在发出 HTTP 下载请求时未设置 cookie。
但是,现在当我创建新的临时 session 时,授权工作正常。
// Part #3
let sessionConfiguration = URLSessionConfiguration.ephemeral
sessionConfiguration.httpCookieStorage = HTTPCookieStorage.shared
session_ = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil);
第 2 部分和第 3 部分在授权(设置 cookie)方面有何区别?为什么第 3 部分成功而第 2 部分失败?
最佳答案
对于你的#1、#2、#3部分添加以下行来请求
request.httpShouldHandleCookies=true
如果仍然出现相同的错误,请咨询您的后端开发人员并检查您尝试下载的文件的下载权限
要获得完整的帮助,您可以检查以下代码
let mainUrl = "your request url"
request.httpMethod = serviceType
request.httpShouldHandleCookies=true
let cookie = HTTPCookie.requestHeaderFields(with: HTTPCookieStorage.shared.cookies(for: mainUrl as URL)!)
request = setAccessToken_Cookie(request: request)
request.allHTTPHeaderFields = cookie
var session = URLSession(configuration: URLSessionConfiguration.default)
session = NetworkManager().configureURLSessionMethodWithDelegate(session: session)
if downloadable
{
Downloader.downloadFile(url: mainUrl as URL, session: session, request:request, completion: {
})
}
import Foundation
import UIKit
class Downloader:NSObject, URLSessionDownloadDelegate
{
class func downloadFile(url: URL,session:URLSession, request:NSMutableURLRequest, completion: @escaping () -> ()) {
var delegateSession = Downloader().configureURLSessionMethodDelegate(session: session)
let downloads = delegateSession.downloadTask(with: request as URLRequest)
downloads.resume()
}
func configureURLSessionMethodDelegate( session:URLSession) -> URLSession {
var sessionWithDelegate = session
sessionWithDelegate = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue:OperationQueue.main)
return sessionWithDelegate
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
guard let httpResponse = downloadTask.response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
print ("error : \(error according to your response code)")
return
}
do {
let documentsURL = try
FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
let savedURL = documentsURL.appendingPathComponent(downloadTask.response?.suggestedFilename ?? "noname")
print(location)
print(savedURL)
if FileManager.default.fileExists(atPath: savedURL.path)
{
AppUtility?.displayAlert(title: appName, messageText: "A file with same name already exist", delegate: nil)
}
else{
try FileManager.default.copyItem(at: location, to: savedURL)
}
} catch {
print ("file error: \(error)")
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
{
guard let httpResponse = downloadTask.response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
print ("server error")
return
}
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
DispatchQueue.main.async(execute: { () -> Void in
if(error != nil)
{
//handle the error
print("Download completed with error: \(error!.localizedDescription)");
}
})
}
}
关于ios - 使用 HTTPCookieStorage.shared 在 URLSessionConfiguration.background 中授权失败(cookes 未发送),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57651965/
我有 FirstViewController,带有要在 SecondTableViewController 中移动的按钮。在 SecondTableViewController 中,我有单元格,如果我
我已经使用默认配置定义了 session 配置,并提供了 tlsMinimumSupportedProtocol 作为 TLSv1.2 我正在使用 Alamofire,但我的应用程序仍然支持较低版本
我得到一个 Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service" 当我在启动
给定以下 Playground : import Foundation let config = URLSessionConfiguration.default let session = URLSe
从 iOS 11 开始我们可以设置: let sessionConfiguration = URLSessionConfiguration.default sessionConfiguration.m
请注意,我已经尝试了很多解决方案,例如 this但都没有用。 这里是 screenshot和代码; func downloadItems() { let urlE: String = "htt
在我的 iOS 应用程序中,我使用默认配置创建一个 URLSession: // Part #1 let urlSession = URLSession(configuration: .defa
我是一名优秀的程序员,十分优秀!