如何使用Alamofire 4和Swift 3的代理服务器

编程入门 行业动态 更新时间:2024-10-22 13:55:48
本文介绍了如何使用Alamofire 4和Swift 3的代理服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请不要标记为重复,我无法使用现有主题解决我的问题。

我'我试图将我的代理用于需要指定IP的API请求。 要调试我的问题,我是从网络服务请求IP。

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.

这是我目前的代码:

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] = "xxx@eu-west-static-01.quotaguard" 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: "api.ipify?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 相同。非常感谢任何帮助。

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

PS:使用的物理设备。

PS: physical device used.

推荐答案

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

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

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" as AnyObject? proxyConfiguration[kCFNetworkProxiesHTTPPort] = "9293" as AnyObject? proxyConfiguration[kCFNetworkProxiesHTTPEnable] = 1 as AnyObject? proxyConfiguration[kCFStreamPropertyHTTPSProxyHost as String] = "eu-west-static-01.quotaguard" 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: "api.ipify?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请求。

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

注意:它可能会给出已弃用警告这些键,但键仍然有效(请参阅 forums.developer.apple/thread/19356#131446 )

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

注意:我发布了相同的答案此处。在这里发布的内容也适用于此处。 Alamofire使用相同的 URLSessionConfiguration 。

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

更多推荐

如何使用Alamofire 4和Swift 3的代理服务器

本文发布于:2023-11-26 17:54:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代理服务器   如何使用   Alamofire   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!