Wednesday, March 15, 2017

How to use a Proxy Server with Alamofire 4 and Swift 3

Leave a Comment

Please don't mark as duplicate, I haven't been able to solve my Issue with the existing threads.

I'm trying to use my Proxy for an API request that needs a specified IP. To debug my issue, I'm requesting the IP from a webservice.

This is my current code:

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

The problem: the responsed IP is the same with or without the proxyConfiguration. Any help is very appreciated.

PS: physical device used.

1 Answers

Answers 1

I think the working (supposed to be deprecated) keys are:

kCFStreamPropertyHTTPSProxyHost kCFStreamPropertyHTTPSProxyPort 

Could you try this code?

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

Also please make sure your proxy server is configured to handle https requests.

Note: It might give deprecated warning for those keys but keys are still working (see https://forums.developer.apple.com/thread/19356#131446)

Note: I posted the same answer here. Posting the same here as it was applicable here as well. Alamofire is using same URLSessionConfiguration.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment