请不要标记为重复,我无法使用现有主题解决我的问题。
我'我试图将我的代理用于需要指定IP的API请求。 要调试我的问题,我是从网络服务请求IP。
I'm trying to use my Proxy for an API request that needs a specified IP. To debug my issue, I'm requesting the IP from a webservice.
这是我目前的代码:
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] = "xxx@eu-west-static-01.quotaguard" 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: "api.ipify?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)") } } } }问题:响应的IP是否与 proxyConfiguration 相同。非常感谢任何帮助。
The problem: the responsed IP is the same with or without the proxyConfiguration. Any help is very appreciated.
PS:使用的物理设备。
PS: physical device used.
推荐答案我认为工作(应该被弃用)键是:
I think the working (supposed to be deprecated) keys are:
kCFStreamPropertyHTTPSProxyHost kCFStreamPropertyHTTPSProxyPort你能试试这个代码吗?
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" as AnyObject? proxyConfiguration[kCFNetworkProxiesHTTPPort] = "9293" as AnyObject? proxyConfiguration[kCFNetworkProxiesHTTPEnable] = 1 as AnyObject? proxyConfiguration[kCFStreamPropertyHTTPSProxyHost as String] = "eu-west-static-01.quotaguard" 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: "api.ipify?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)") } } } }另外请确保您的代理服务器配置为处理https请求。
Also please make sure your proxy server is configured to handle https requests.
注意:它可能会给出已弃用警告这些键,但键仍然有效(请参阅 forums.developer.apple/thread/19356#131446 )
Note: It might give deprecated warning for those keys but keys are still working (see forums.developer.apple/thread/19356#131446)
注意:我发布了相同的答案此处。在这里发布的内容也适用于此处。 Alamofire使用相同的 URLSessionConfiguration 。
Note: I posted the same answer here. Posting the same here as it was applicable here as well. Alamofire is using same URLSessionConfiguration.
更多推荐
如何使用Alamofire 4和Swift 3的代理服务器
发布评论