func makeRequestViaUrlSessionProxy(_ url: String,completion: @escaPing (_ result: String?) -> ()) { let request = URLRequest(url: URL(string: url)!) let config = URLSessionConfiguration.default config.requestCachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData config.connectionProxyDictionary = [AnyHashable: Any]() config.connectionProxyDictionary?[kcfNetworkProxIEshttpEnable as String] = 1 config.connectionProxyDictionary?[kcfNetworkProxIEshttpProxy as String] = "142.54.173.19" config.connectionProxyDictionary?[kcfNetworkProxIEshttpPort as String] = 8888 let session = URLSession.init(configuration: config,delegate: nil,delegateQueue: OperationQueue.current) let task = session.dataTask(with: request) { (data: Data?,response: URLResponse?,error: Error?) in if error != nil { NSLog("ClIEnt-sIDe error in request to \(url): \(error)") completion(nil) return } if data == nil { NSLog("Data from request to \(url) is nil") completion(nil) return } let httpResponse = response as? httpURLResponse if httpResponse?.statusCode != 200 { NSLog("Server-sIDe error in request to \(url): \(httpResponse)") completion(nil) return } let enCodingname = response?.textEnCodingname != nil ? response?.textEnCodingname : "utf-8" let enCoding = CFStringConvertEnCodingTonsstringencoding(CFStringConvertIANACharSetnameToEnCoding(enCodingname as CFString!)) let stringData = String(data: data!,enCoding: String.EnCoding(rawValue: UInt(enCoding))) session.invalIDateAndCancel() completion(stringData) } task.resume()}
调用者:
overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() makeRequestViaUrlSessionProxy("https://API.ipify.org?format=Json") { string in print(string) }}
看起来配置完全被忽略了,因为即使使用了代理IP,响应IP始终是实际的设备IP
任何帮助是非常感谢.
编辑:按照用户hasan83的建议,采取“httpS密钥”似乎不是一个选择.
kcfStreamPropertyhttpSProxyHostkcfStreamPropertyhttpSProxyPort
你可以试试这个代码吗?
func makeRequestViaUrlSessionProxy(_ url: String,completion: @escaPing (_ result: String?) -> ()) { let request = URLRequest(url: URL(string: url)!) let config = URLSessionConfiguration.default config.requestCachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData config.connectionProxyDictionary = [AnyHashable: Any]() config.connectionProxyDictionary?[kcfNetworkProxIEshttpEnable as String] = 1 config.connectionProxyDictionary?[kcfNetworkProxIEshttpProxy as String] = "142.54.173.19" config.connectionProxyDictionary?[kcfNetworkProxIEshttpPort as String] = 8888 config.connectionProxyDictionary?[kcfStreamPropertyhttpSProxyHost as String] = "142.54.173.19" config.connectionProxyDictionary?[kcfStreamPropertyhttpSProxyPort as String] = 8888 let session = URLSession.init(configuration: config,enCoding: String.EnCoding(rawValue: UInt(enCoding))) session.invalIDateAndCancel() completion(stringData) } task.resume()}
还请确保您的代理服务器配置为处理https请求.
注意:它可能会为这些键提供不推荐的警告,但键仍然工作(请参阅https://forums.developer.apple.com/thread/19356#131446)
总结以上是内存溢出为你收集整理的ios – 如何在Swift 3中使用代理服务器的URLSession全部内容,希望文章能够帮你解决ios – 如何在Swift 3中使用代理服务器的URLSession所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)