ios – 如何使用代理服务器与Alamofire 4和Swift 3

ios – 如何使用代理服务器与Alamofire 4和Swift 3,第1张

概述请不要标记为重复,我无法使用现有线程解决我的问题. 我正在尝试使用我的Proxy来获取需要指定IP的API请求. 要调试我的问题,我正在从Web服务请求IP. 这是我当前的代码: import UIKitimport Alamofireclass ViewController: UIViewController { var requestManager = Alamofire.Se 请不要标记为重复,我无法使用现有线程解决我的问题.

我正在尝试使用我的Proxy来获取需要指定IP的API请求.
要调试我的问题,我正在从Web服务请求IP.

这是我当前的代码:

import UIKitimport Alamofireclass 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)")            }        }    }}

问题:响应的IP与或不与proxyConfiguration相同.任何帮助是非常感谢.

PS:使用物理设备

解决方法 我认为工作(应该被弃用)的键是:
kcfStreamPropertyhttpSProxyHostkcfStreamPropertyhttpSProxyPort

你可以试试这个代码吗?

import UIKitimport Alamofireclass 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,enCoding: .utf8) {                print("Data: \(utf8Text)")            }        }    }}

还请确保您的代理服务器配置为处理https请求.

注意:它可能会为这些键提供不推荐的警告,但键仍然工作(请参阅https://forums.developer.apple.com/thread/19356#131446)

注意:我发布了同样的答案here.同样适用于这里. Alamofire正在使用相同的URLSessionConfiguration.

总结

以上是内存溢出为你收集整理的ios – 如何使用代理服务器与Alamofire 4和Swift 3全部内容,希望文章能够帮你解决ios – 如何使用代理服务器与Alamofire 4和Swift 3所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110441.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存