bru*_*del 2 proxy http ios swift swift-playground
我需要连接到 HTTP 代理后面的网络服务器。我尝试过使用URLSessionConfigurationwith connectionProxyDictionary,但似乎我的代理配置被忽略,因为您可以将代理主机值更改为任何值,结果是相同的。
我尝试使用Charles Proxy代替我需要连接的代理来调试情况,但发现请求从未到达它,所以我相信我的客户端代码有问题。
\n\nimport Foundation\n\nclass URLSessionProxy: NSObject, URLSessionDelegate {\n\n func doRequest() {\n\n let configuration = URLSessionConfiguration.default\n configuration.connectionProxyDictionary = [\n kCFNetworkProxiesHTTPEnable: true,\n kCFNetworkProxiesHTTPProxy: "localhost",\n kCFNetworkProxiesHTTPPort: "8888",\n ]\n\n var request = URLRequest(url: URL(string: "https://ip.seeip.org/jsonip")!)\n request.addValue("application/json", forHTTPHeaderField: "Accept")\n\n URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)\n .dataTask(with: request, completionHandler: { (data, response, error) in\n if error == nil {\n print("url request = ", request.url?.absoluteString)\n print("headers request = ", request.allHTTPHeaderFields.debugDescription)\n print("response = ", response)\n print("data body = ", String(data: data!, encoding: String.Encoding.utf8.self))\n\n } else {\n print("error = ", error)\n print(error?.localizedDescription)\n }\n }).resume()\n }\n}\n\nURLSessionProxy().doRequest()\nRun Code Online (Sandbox Code Playgroud)\n\n在给出的示例中,我希望使用我在 localhost:8888.\xe2\x80\x8b 上设置的代理间接连接到 ip.seeip.org
\n\n要点: https: //gist.github.com/brunabaudel/90a6873d2c3df6caeb89f8b7afc9adce
\n这些kCFNetworkProxiesHTTP键仅控制用于httpURL 的代理。httpsURL 使用通过键定义的代理kCFNetworkProxiesHTTPS。
不幸的是,HTTPS 密钥在 iOS 上不可用(真的不知道为什么),但实现已经存在,因此您可以只传递带有正确密钥的字符串,即"HTTPSEnable"、"HTTPSProxy"和"HTTPSPort"。