如何在SWIFT 3中向NSURLSession添加HTTPS代理

时间:2016-10-25 17:21:41

标签: ios swift proxy ios9 http-proxy

我使用以下代码连接到代理服务器,仅适用于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){}

1 个答案:

答案 0 :(得分:0)

我找到了向NSURLsession添加HTTPS代理的解决方案

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

    let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration();