2017-03-06 60 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,並可能隨時被刪除。通過使用字符串值,代碼可能會中斷,但不會崩潰

相關問題