2017-03-06 62 views
12

请不要标记为重复,我还没有能够解决我的问题与现有的线程。如何使用代理服务器与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)") 
      } 
     } 
    } 
} 

的问题:responsed IP是有或没有proxyConfiguration相同。任何帮助非常感谢。

PS:使用的物理设备。

回答

9

我认为工作(应该是不建议使用)键:

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

+0

添加用户名和密码的属性键。我认为这些也是必需的,以使其工作。 – manishg

1

基于关闭Manishg的答案,我使用以下方法来避免警告

let configuration = URLSessionConfiguration.default 
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders 

var proxyConfiguration = [String: AnyObject]() 
proxyConfiguration.updateValue(1 as AnyObject, forKey: "HTTPEnable") 
proxyConfiguration.updateValue("eu-west-static-01.quotaguard.com" as AnyObject, forKey: "HTTPProxy") 
proxyConfiguration.updateValue(9293 as AnyObject, forKey: "HTTPPort") 
proxyConfiguration.updateValue(1 as AnyObject, forKey: "HTTPSEnable") 
proxyConfiguration.updateValue("eu-west-static-01.quotaguard.com" as AnyObject, forKey: "HTTPSProxy") 
proxyConfiguration.updateValue(9293 as AnyObject, forKey: "HTTPSPort") 
configuration.connectionProxyDictionary = proxyConfiguration 

sharedManager = Alamofire.SessionManager(configuration: configuration) 

常量已过时的HTTPS,并可能随时被删除。通过使用字符串值,代码可能会中断,但不会崩溃

相关问题