如何在SWIFT 3中将HTTPS代理添加到NSURLSession
我已经使用下面的代码连接到代理服务器,仅适用于HTTP请求,但不适用于HTTPS。 在iOS 9.0中, kCFStreamPropertyHTTPSProxyHost和kCFStreamPropertyHTTPSProxyPort被折旧。
let myPortInt = 12345; let myProxyUrlString = "myProxyURL"; let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration(); sessionConfiguration.connectionProxyDictionary = [ kCFNetworkProxiesHTTPEnable: true, kCFNetworkProxiesHTTPPort: myPortInt, kCFNetworkProxiesHTTPProxy: myProxyUrlString, ] let url = NSURL(string: endPointUrl) let postRequest = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0) let jsonString = bodyString postRequest.HTTPBody = jsonString postRequest.HTTPMethod = "POST" postRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") let session = NSURLSession(configuration: sessionConfiguration) let task = session.dataTaskWithRequest(postRequest){}
我find了向NSURLsession添加HTTPS代理的解决scheme
let proxyDict : NSDictionary = ["HTTPEnable": Int(1), "HTTPProxy": myProxyUrlString, "HTTPPort": myPortInt, "HTTPSEnable": Int(1), "HTTPSProxy": myProxyUrlString, "HTTPSPort": myPortInt] let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration();