How to add a HTTPS proxy to NSURLSession in SWIFT 3

余生长醉 提交于 2020-01-22 16:19:52

问题


I have used the following code for connecting to the proxy server and works great for only HTTP requests but not for HTTPS. In iOS 9.0, kCFStreamPropertyHTTPSProxyHost and kCFStreamPropertyHTTPSProxyPort are depreciated.

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

回答1:


I found the solution for adding HTTPS proxy to NSURLsession

   let proxyDict : NSDictionary = ["HTTPEnable": Int(1), "HTTPProxy": myProxyUrlString, "HTTPPort": myPortInt, "HTTPSEnable": Int(1), "HTTPSProxy": myProxyUrlString, "HTTPSPort": myPortInt]

    let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration();


来源:https://stackoverflow.com/questions/40246010/how-to-add-a-https-proxy-to-nsurlsession-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!