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

前端之家收集整理的这篇文章主要介绍了ios – 如何使用代理服务器与Alamofire 4和Swift 3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
请不要标记为重复,我无法使用现有线程解决我的问题.

我正在尝试使用我的Proxy来获取需要指定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)")
            }
        }
    }
}

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

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

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

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

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

原文链接:https://www.f2er.com/iOS/335857.html

猜你在找的iOS相关文章