对swift的URLSession进行拆分,增加支持设置resolve和connectTo的能力
使用的时候,对URLRequest设定Header
let resolve = "\(host):443:\(ip)"
setValue(resolve, forHTTPHeaderField: "resolve")connectTo:
let connectTo = "\(host):443:\(ip):443"
setValue(connectTo, forHTTPHeaderField: "connectTo")支持SNI功能,用于IP直连
目前发现上面两个字段都可以实现强制解析的功能 所以用上面那个字段就可以实现需求了 需要注意的是,ip服务器需要包含host域名的证书,这样才能认证通过
.package(url: "https://github.com/zhtut/SSURLSession.git", from: "0.1.0")pod 'SSURLSession'import SSURLSession
let host = "www.baidu.com"
let urlStr = "https://\(host)"
let url = URL(string: urlStr)!
var req = URLRequest(url: url)
// direct ip
let ip = "14.119.104.254"
let resolve = "\(host):443:\(ip)"
// add dns resolve
req.setValue(resolve, forHTTPHeaderField: "resolve")
let task = SSURLSession.URLSession.shared.dataTask(with: req, completionHandler: { data, resp, error in
if let data {
print("success")
} else {
if let error {
print("error: \(error)")
}
}
})
task.resume()由于swift foundation core lib 未完全完成,有部分功能是不正常的, 但至少比CFNetwork和直接使用libcurl更成熟
https://github.com/apple/swift-corelibs-foundation/blob/main/Docs/Status.md
-
URL: Networking primitives
The classes in this group provide functionality for manipulating URLs and paths via a common model object. The group also has classes for creating and receiving network connections.
Entity Name Status Test Coverage Notes URLAuthenticationChallengeUnimplemented None URLCacheUnimplemented None URLCredentialComplete Incomplete URLCredentialStorageUnimplemented None NSURLError*Complete N/A URLProtectionSpaceUnimplemented None URLProtocolUnimplemented None URLProtocolClientUnimplemented None NSURLRequestComplete Incomplete NSMutableURLRequestComplete Incomplete URLResponseComplete Substantial NSHTTPURLResponseComplete Substantial NSURLMostly Complete Substantial Resource values remain unimplemented NSURLQueryItemComplete N/A URLResourceKeyComplete N/A URLFileResourceTypeComplete N/A URLComplete Incomplete URLResourceValuesComplete N/A URLComponentsComplete Incomplete URLRequestComplete None HTTPCookieComplete Incomplete HTTPCookiePropertyKeyComplete N/A HTTPCookieStorageMostly Complete Substantial HostComplete None ConfigurationN/A N/A For internal use only EasyHandleN/A N/A For internal use only HTTPBodySourceN/A N/A For internal use only HTTPMessageN/A N/A For internal use only libcurlHelpersN/A N/A For internal use only MultiHandleN/A N/A For internal use only URLSessionMostly Complete Incomplete invalidation, resetting, flushing, getting tasks, and others remain unimplemented URLSessionConfigurationMostly Complete Incomplete ephemeralandbackground(withIdentifier:)remain unimplementedURLSessionDelegateComplete N/A URLSessionTaskMostly Complete Incomplete cancel(),createTransferState(url:)with streams, and others remain unimplementedURLSessionDataTaskComplete Incomplete URLSessionUploadTaskComplete None URLSessionDownloadTaskIncomplete Incomplete URLSessionStreamTaskUnimplemented None TaskRegistryN/A N/A For internal use only TransferStateN/A N/A For internal use only