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

标签 ios swift alamofire

我正在尝试将我的代理用于需要指定 IP 的 API 请求。 为了调试我的问题,我从网络服务请求 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)")
            }
        }
    }
}

问题:无论有没有proxyConfiguration,响应的IP都是一样的。

PS:使用的是物理设备。

最佳答案

我认为工作(应该被弃用)的键是:

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 )

注意:我发布了相同的答案 here .在此处发布相同内容,因为它也适用于此处。 Alamofire 使用相同的 URLSessionConfiguration

关于ios - 如何在 Alamofire 4 和 Swift 3 中使用代理服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616908/

相关文章:

ios - 从 navigationItem 中删除项目以编程方式显示 navigationItems 标题

ios - Django 休息框架日期时间字段格式

ios - 后退按钮未出现在 UIViewController 中

ios - 在 Init 中调用函数设置变量

ios - 在完成处理程序中执行 Segue

swift - 在 HTTP POST 请求中传递多个参数

iOS - 应用程序因特定 Controller 而崩溃

ios - -[CCTextureAtlas drawNumberOfQuads :fromIndex:] 556 中的 OpenGL 错误 0x0500

ios - Alamofire:支持上传包含文本和文件部分的多部分表单吗?

alamofire - 错误域=NSPOSIXErrorDomain 代码=28 “No space left on device” UserInfo={_kCFStreamErrorCodeKey=28, _kCFStreamErrorDomainKey=1}