如何在Alamofire 4和Swift 3中使用代理服务器

大卫·希克

请不要将其标记为重复项,因为我无法使用现有线程解决问题。

我正在尝试将我的代理用于需要指定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://[email protected]" 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:使用的物理设备。

Manishg

我认为工作键(假定不推荐使用)为:

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.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)")
            }
        }
    }
}

另外,请确保将您的代理服务器配置为处理https请求。

注意:它可能会deprecated警告那些键,但是键仍在工作(请参阅https://forums.developer.apple.com/thread/19356#131446

注意:我在此处发布了相同的答案在此处发布与此处同样适用的相同内容。Alamofire使用的是相同的URLSessionConfiguration

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Selenium和Chrome设置代理服务器

如何在JUnit4中使用ClassRule启动Spring Boot服务器?

如何在Nginx代理服务器中启用CORS?

如何在swift5和Alamofire 5 Beta版本中获取和保存响应?

如何配置Maven不使用代理服务器

如何在django和psycopg2中使用服务器端游标?

Swift 3如何使用SSL Pinning和AlamoFire验证服务器证书?

如何在Swift Alamofire中使用SSL证书?

如何在Nginx中更改代理服务器响应的状态码?

如何使用Node JS Puppeteer在无头chrome请求中设置代理服务器

如何在Swift 4中从Alamofire解析数据

如何在代理服务器上使用axios进行https呼叫?

如何在iOS Swift中使用Alamofire获得JSON响应?

如何使用symfony / panther设置代理服务器

如何在swiftyJSON和Alamofire中获得字典的随机值

如何在iOS Swift 4中处理Alamofire响应?

将请求发送到代理服务器时如何获取目标端口和IP?

如何获取客户端的本地IP地址和代理服务器

如何在不安装任何软件的情况下使用代理服务器

Nginx和Apache2(代理服务器)

如何在Xubuntu,Lubuntu或Ubuntu Studio中设置系统范围的代理服务器?

如何在 Alamofire 请求 swift 3 中传递 JSON 对象

如何确保我的应用程序使用的是代理服务器而不是我在 selenium 中的 IP 地址?

在 swift 4 中使用 Alamofire 发出请求时,来自服务器的 JSON 解析错误

Swift 3:如何在 ALamofire 4 的请求正文中添加或删除参数?

如何在scrapy中限制对站点而不是代理服务器的请求?

如何在 Docker for Windows 中的 Linux Container 中设置代理服务器

如何在 swift 5 中使用 Alamofire multipartFormData 发送数据和图像

如何在代码中为 RestClient API 设置代理服务器