Mac开发----让请求走代理(URLSessionConfiguration Proxy)

话不多说,直接上代码:

//设置HTTP代理
let sessionConfiguration: URLSessionConfiguration = .default

let proxyConfiguration: [AnyHashable : Any] = [
		kCFNetworkProxiesHTTPEnable: true,
		kCFNetworkProxiesHTTPPort: 1080, //代理端口
		kCFNetworkProxiesHTTPProxy: "127.0.0.1" //代理地址
]

sessionConfiguration.connectionProxyDictionary = proxyConfiguration

let session = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)


//设置HTTPS代理:
let sessionConfiguration: URLSessionConfiguration = .default

let proxyConfiguration: [AnyHashable : Any] = [
		kCFNetworkProxiesHTTPSEnable: true,
		kCFNetworkProxiesHTTPSPort: 1080, //代理端口
		kCFNetworkProxiesHTTPSProxy: "127.0.0.1" //代理地址
]

sessionConfiguration.connectionProxyDictionary = proxyConfiguration

let session = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)

//设置SOCKS代理:
let sessionConfiguration: URLSessionConfiguration = .default

let proxyConfiguration: [AnyHashable : Any] = [
		kCFNetworkProxiesSOCKSEnable: true,
		kCFNetworkProxiesSOCKSPort: 1080, //代理端口
		kCFNetworkProxiesSOCKSProxy: "127.0.0.1" //代理地址
]

sessionConfiguration.connectionProxyDictionary = proxyConfiguration

let session = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)

之后使用设置好代理的session就可以让请求走代理了。
要取消代理怎么办?
你把kCFNetXXXXXenable设置成false就行了啊。

其实代码都一样,只是参数不同:

Raw值CFNetwork/CFProxySupport.hCFNetwork/CFHTTPStream.h CFNetwork/CFSocketStream.h
“HTTPEnable”kCFNetworkProxiesHTTPEnableN/A
“HTTPProxy”kCFNetworkProxiesHTTPProxykCFStreamPropertyHTTPProxyHost
“HTTPPort”kCFNetworkProxiesHTTPPortkCFStreamPropertyHTTPProxyPort
“HTTPSEnable”kCFNetworkProxiesHTTPSEnableN/A
“HTTPSProxy”kCFNetworkProxiesHTTPSProxykCFStreamPropertyHTTPSProxyHost
“HTTPSPort”kCFNetworkProxiesHTTPSPortkCFStreamPropertyHTTPSProxyPort
“SOCKSEnable”kCFNetworkProxiesSOCKSEnableN/A
“SOCKSProxy”kCFNetworkProxiesSOCKSProxykCFStreamPropertySOCKSProxyHost
“SOCKSPort”kCFNetworkProxiesSOCKSPortkCFStreamPropertySOCKSProxyPort

可以配合Alamofire使用:

import Cocoa
import Alamofire

class Network {
    private static let requestQueue = DispatchQueue(label: "Network")
    private static let sharedProxySession: Session = {
        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 5
        if let a = UserDefaults.standard.string(forKey: USERDEFAULTS_LOCAL_SOCKS5_LISTEN_ADDRESS), 
        	let p = UserDefaults.standard.value(forKey: USERDEFAULTS_LOCAL_SOCKS5_LISTEN_PORT) as? NSNumber {
        	
            let proxyConfiguration: [AnyHashable : Any] = [kCFNetworkProxiesSOCKSEnable : true, 
            											   kCFNetworkProxiesSOCKSProxy: a, 
            											   kCFNetworkProxiesSOCKSPort: p.intValue]
            configuration.connectionProxyDictionary = proxyConfiguration
        }
        return Session(configuration: configuration, rootQueue: DispatchQueue.main, startRequestsImmediately: true, requestQueue: Network.requestQueue)
    }()
    
    private static let sharedSession: Session = {
        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 5
        let proxyConfiguration: [AnyHashable : Any] = [kCFNetworkProxiesSOCKSEnable : false]
        configuration.connectionProxyDictionary = proxyConfiguration
        return Session(configuration: configuration, rootQueue: DispatchQueue.main, startRequestsImmediately: true, requestQueue: Network.requestQueue)
    }()
    
    static func session(useProxy: Bool) -> Session {
        if useProxy {
            return sharedProxySession
        } else {
            return sharedSession
        }
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页