如何以编程方式将代理添加到 NSURLSession
How to programmatically add a proxy to an NSURLSession
查看 NSURLSession
和 NSURLSessionConfiguration
的文档,我的印象是我应该使用如下字典配置它:
// Create a dictionary to describe the proxy
NSDictionary *proxyDict = @{
(NSString *)kCFProxyHostNameKey : @"myProxyHost.com",
(NSString *)kCFProxyPortNumberKey : @"12345",
(NSString *)kCFProxyTypeKey : (NSString*)kCFProxyTypeHTTP
};
// Create a configuration that uses the dictionary
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[configuration setConnectionProxyDictionary:proxyDict];
但是,使用此配置创建的 NSURLSession
的请求直接连接。
事实证明,你想要的字典键是 Stream 变体,它们是解析为 "HTTPProxy" 的那些:
NSString* proxyHost = @"myProxyHost.com";
NSNumber* proxyPort = [NSNumber numberWithInt: 12345];
// Create an NSURLSessionConfiguration that uses the proxy
NSDictionary *proxyDict = @{
@"HTTPEnable" : [NSNumber numberWithInt:1],
(NSString *)kCFStreamPropertyHTTPProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPProxyPort : proxyPort,
@"HTTPSEnable" : [NSNumber numberWithInt:1],
(NSString *)kCFStreamPropertyHTTPSProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPort,
};
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
configuration.connectionProxyDictionary = proxyDict;
// Create a NSURLSession with our proxy aware configuration
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// Form the request
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com?2"]];
// Dispatch the request on our custom configured session
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"NSURLSession got the response [%@]", response);
NSLog(@"NSURLSession got the data [%@]", data);
}];
NSLog(@"Lets fire up the task!");
[task resume];
由于 Jeff 的几个答案已被弃用,我使用这个
NSMutableDictionary *proxy=[NSMutableDictionary dictionaryWithDictionary:@{(__bridge NSString *)kCFNetworkProxiesHTTPEnable : @1, (__bridge NSString *)kCFNetworkProxiesHTTPSEnable : @1}];
proxy[(__bridge NSString *)kCFNetworkProxiesHTTPProxy] =host;
proxy[(__bridge NSString *)kCFNetworkProxiesHTTPSProxy]=host;
proxy[(__bridge NSString *)kCFNetworkProxiesHTTPPort] =port;
proxy[(__bridge NSString *)kCFNetworkProxiesHTTPSPort] =port;
proxy[(__bridge NSString *)kCFProxyUsernameKey]=user;
proxy[(__bridge NSString *)kCFProxyPasswordKey]=password;
configuration.connectionProxyDictionary=proxy;
如果有人需要这个的 swift 版本:
Swift 3
let sessionConfiguration = URLSessionConfiguration.default
sessionConfiguration.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable as AnyHashable: true,
kCFNetworkProxiesHTTPPort as AnyHashable: 999, //myPortInt
kCFNetworkProxiesHTTPProxy as AnyHashable: "myProxyUrlString"
]
let session = URLSession(configuration: sessionConfiguration)
kCFProxyPortNumberKey
值应该是 Int
而不是 String
Swift 3 分机
extension URLSession {
func withProxy(proxyURL: String, proxyPort: Int) -> URLSession {
var configuration = self.configuration
configuration.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable as AnyHashable : true,
kCFNetworkProxiesHTTPPort as AnyHashable : proxyPort,
kCFNetworkProxiesHTTPProxy as AnyHashable : proxyURL
]
return URLSession(configuration: configuration, delegate: self.delegate, delegateQueue: self.delegateQueue)
}
}
用法:
let session = URLSession().withProxy(proxyURL: "xxxxxx", proxyPort: 8321)
根据之前的所有回复,这适用于 Swift 4,HTTP 和 HTTPS:
let proxyHost = "127.0.0.1"
let proxyPort = 8888
let configuration = URLSessionConfiguration.default
configuration.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable: true,
kCFNetworkProxiesHTTPProxy: proxyHost,
kCFNetworkProxiesHTTPPort: proxyPort,
kCFNetworkProxiesHTTPSEnable: true,
kCFNetworkProxiesHTTPSProxy: proxyHost,
kCFNetworkProxiesHTTPSPort: proxyPort
]
proxyHost
应该是主机名并且不包含任何 URL 方案。
查看 NSURLSession
和 NSURLSessionConfiguration
的文档,我的印象是我应该使用如下字典配置它:
// Create a dictionary to describe the proxy
NSDictionary *proxyDict = @{
(NSString *)kCFProxyHostNameKey : @"myProxyHost.com",
(NSString *)kCFProxyPortNumberKey : @"12345",
(NSString *)kCFProxyTypeKey : (NSString*)kCFProxyTypeHTTP
};
// Create a configuration that uses the dictionary
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[configuration setConnectionProxyDictionary:proxyDict];
但是,使用此配置创建的 NSURLSession
的请求直接连接。
事实证明,你想要的字典键是 Stream 变体,它们是解析为 "HTTPProxy" 的那些:
NSString* proxyHost = @"myProxyHost.com";
NSNumber* proxyPort = [NSNumber numberWithInt: 12345];
// Create an NSURLSessionConfiguration that uses the proxy
NSDictionary *proxyDict = @{
@"HTTPEnable" : [NSNumber numberWithInt:1],
(NSString *)kCFStreamPropertyHTTPProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPProxyPort : proxyPort,
@"HTTPSEnable" : [NSNumber numberWithInt:1],
(NSString *)kCFStreamPropertyHTTPSProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPort,
};
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
configuration.connectionProxyDictionary = proxyDict;
// Create a NSURLSession with our proxy aware configuration
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// Form the request
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com?2"]];
// Dispatch the request on our custom configured session
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"NSURLSession got the response [%@]", response);
NSLog(@"NSURLSession got the data [%@]", data);
}];
NSLog(@"Lets fire up the task!");
[task resume];
由于 Jeff 的几个答案已被弃用,我使用这个
NSMutableDictionary *proxy=[NSMutableDictionary dictionaryWithDictionary:@{(__bridge NSString *)kCFNetworkProxiesHTTPEnable : @1, (__bridge NSString *)kCFNetworkProxiesHTTPSEnable : @1}];
proxy[(__bridge NSString *)kCFNetworkProxiesHTTPProxy] =host;
proxy[(__bridge NSString *)kCFNetworkProxiesHTTPSProxy]=host;
proxy[(__bridge NSString *)kCFNetworkProxiesHTTPPort] =port;
proxy[(__bridge NSString *)kCFNetworkProxiesHTTPSPort] =port;
proxy[(__bridge NSString *)kCFProxyUsernameKey]=user;
proxy[(__bridge NSString *)kCFProxyPasswordKey]=password;
configuration.connectionProxyDictionary=proxy;
如果有人需要这个的 swift 版本:
Swift 3
let sessionConfiguration = URLSessionConfiguration.default
sessionConfiguration.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable as AnyHashable: true,
kCFNetworkProxiesHTTPPort as AnyHashable: 999, //myPortInt
kCFNetworkProxiesHTTPProxy as AnyHashable: "myProxyUrlString"
]
let session = URLSession(configuration: sessionConfiguration)
kCFProxyPortNumberKey
值应该是 Int
而不是 String
Swift 3 分机
extension URLSession {
func withProxy(proxyURL: String, proxyPort: Int) -> URLSession {
var configuration = self.configuration
configuration.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable as AnyHashable : true,
kCFNetworkProxiesHTTPPort as AnyHashable : proxyPort,
kCFNetworkProxiesHTTPProxy as AnyHashable : proxyURL
]
return URLSession(configuration: configuration, delegate: self.delegate, delegateQueue: self.delegateQueue)
}
}
用法:
let session = URLSession().withProxy(proxyURL: "xxxxxx", proxyPort: 8321)
根据之前的所有回复,这适用于 Swift 4,HTTP 和 HTTPS:
let proxyHost = "127.0.0.1"
let proxyPort = 8888
let configuration = URLSessionConfiguration.default
configuration.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable: true,
kCFNetworkProxiesHTTPProxy: proxyHost,
kCFNetworkProxiesHTTPPort: proxyPort,
kCFNetworkProxiesHTTPSEnable: true,
kCFNetworkProxiesHTTPSProxy: proxyHost,
kCFNetworkProxiesHTTPSPort: proxyPort
]
proxyHost
应该是主机名并且不包含任何 URL 方案。