Dav*_*eek 14 ios swift alamofire
请不要标记为重复,我无法用现有的线程解决我的问题.
我正在尝试将我的代理用于需要指定IP的API请求.为了调试我的问题,我从Web服务请求IP.
这是我目前的代码:
import UIKit
import Alamofire
class ViewController: UIViewController {
var requestManager = Alamofire.SessionManager.default
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
var proxyConfiguration = [NSObject: AnyObject]()
proxyConfiguration[kCFNetworkProxiesHTTPProxy] = "http://xxx@eu-west-static-01.quotaguard.com" as AnyObject?
proxyConfiguration[kCFNetworkProxiesHTTPPort] = "9293" as AnyObject?
proxyConfiguration[kCFNetworkProxiesHTTPEnable] = 1 as AnyObject?
let cfg = Alamofire.SessionManager.default.session.configuration
cfg.connectionProxyDictionary = proxyConfiguration
let ip = URL(string: "https://api.ipify.org?format=json")
requestManager = Alamofire.SessionManager(configuration: cfg)
requestManager.request(ip!).response { response in
print("Request: \(response.request)")
print("Response: \(response.response)")
print("Error: \(response.error)")
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:无论是否有响应的IP都是相同的proxyConfiguration.非常感谢任何帮助.
PS:使用的物理设备.
man*_*shg 10
我认为工作(应该被弃用)键是:
kCFStreamPropertyHTTPSProxyHost
kCFStreamPropertyHTTPSProxyPort
Run Code Online (Sandbox Code Playgroud)
你能试试这段代码吗?
import UIKit
import Alamofire
class ViewController: UIViewController {
var requestManager = Alamofire.SessionManager.default
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
var proxyConfiguration = [NSObject: AnyObject]()
proxyConfiguration[kCFNetworkProxiesHTTPProxy] = "eu-west-static-01.quotaguard.com" as AnyObject?
proxyConfiguration[kCFNetworkProxiesHTTPPort] = "9293" as AnyObject?
proxyConfiguration[kCFNetworkProxiesHTTPEnable] = 1 as AnyObject?
proxyConfiguration[kCFStreamPropertyHTTPSProxyHost as String] = "eu-west-static-01.quotaguard.com"
proxyConfiguration[kCFStreamPropertyHTTPSProxyPort as String] = 9293
proxyConfiguration[kCFProxyUsernameKey as String] = xxx
//proxyConfiguration[kCFProxyPasswordKey as String] = "pwd if any"
let cfg = Alamofire.SessionManager.default.session.configuration
cfg.connectionProxyDictionary = proxyConfiguration
let ip = URL(string: "https://api.ipify.org?format=json")
requestManager = Alamofire.SessionManager(configuration: cfg)
requestManager.request(ip!).response { response in
print("Request: \(response.request)")
print("Response: \(response.response)")
print("Error: \(response.error)")
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,请确保您的代理服务器配置为处理https请求.
注意:它可能会对deprecated这些密钥发出警告但密钥仍然有效(请参阅https://forums.developer.apple.com/thread/19356#131446)
注意:我在这里发布了相同的答案.在这里发布的内容与此处适用的相同.Alamofire正在使用它URLSessionConfiguration.
根据 Manishg 的回答,我使用以下内容来避免警告
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
var proxyConfiguration = [String: AnyObject]()
proxyConfiguration.updateValue(1 as AnyObject, forKey: "HTTPEnable")
proxyConfiguration.updateValue("eu-west-static-01.quotaguard.com" as AnyObject, forKey: "HTTPProxy")
proxyConfiguration.updateValue(9293 as AnyObject, forKey: "HTTPPort")
proxyConfiguration.updateValue(1 as AnyObject, forKey: "HTTPSEnable")
proxyConfiguration.updateValue("eu-west-static-01.quotaguard.com" as AnyObject, forKey: "HTTPSProxy")
proxyConfiguration.updateValue(9293 as AnyObject, forKey: "HTTPSPort")
configuration.connectionProxyDictionary = proxyConfiguration
sharedManager = Alamofire.SessionManager(configuration: configuration)
Run Code Online (Sandbox Code Playgroud)
https 常量已被弃用,可以随时删除。通过使用字符串值,代码可能会中断但不会崩溃
| 归档时间: |
|
| 查看次数: |
2286 次 |
| 最近记录: |