2015-01-23 13 views
10

Nhìn qua tài liệu của NSURLSessionNSURLSessionConfiguration, tôi đã theo ấn tượng tôi nên cấu hình nó với một cuốn từ điển như sau:Làm thế nào để lập trình thêm một proxy để một NSURLSession

// 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]; 

Tuy nhiên, các yêu cầu từ NSURLSession được tạo với cấu hình này kết nối trực tiếp.

Trả lời

17

Hóa ra, các phím từ điển bạn muốn là những biến thể Stream, họ là những người mà giải quyết xuống "HTTPProxy" và như vậy:

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]; 
+0

Cảm ơn bạn! Một triệu lần cảm ơn bạn! Tôi sẽ không bao giờ tìm ra điều này. Chắc chắn sẽ là tốt đẹp nếu Apple tài liệu này. – bugloaf

+0

Cảm ơn rất nhiều! Bạn là một phao cứu sinh! –

+0

@ Jeff - Tôi đã thử điều này .. Nhưng dữ liệu trở về từ '^ (dữ liệu NSData *, phản hồi NSURLResponse *, lỗi NSError *)' cung cấp kết quả của myProxyHost.com chứ không phải www.google.com Bạn có thể giúp tôi –

6

Kể từ khi một số phím câu trả lời của Jeff đã bị phản đối tôi sử dụng này những

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; 
7

Nếu ai cần phiên bản nhanh chóng điều này:

Swift 3

giá trị
let sessionConfiguration = URLSessionConfiguration.default 
sessionConfiguration.connectionProxyDictionary = [ 
    kCFNetworkProxiesHTTPEnable as AnyHashable: true, 
    kCFNetworkProxiesHTTPPort as AnyHashable: 999, //myPortInt 
    kCFNetworkProxiesHTTPProxy as AnyHashable: "myProxyUrlString" 
] 
let session = URLSession(configuration: sessionConfiguration) 
+0

Tôi đang gặp khó khăn khi sử dụng. ngay cả khi được sử dụng, ip của yêu cầu api của tôi cũng giống như được sử dụng mà không cần proxy ... –

+0

http://stackoverflow.com/questions/42617582/how-to-use-urlsession-with-proxy-in-swift-3 –

4

kCFProxyPortNumberKey nên Int không String

1

Swift 3 mở rộng

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

Cách sử dụng:

let session = URLSession().withProxy(proxyURL: "xxxxxx", proxyPort: 8321) 
1

Dựa trên tất cả các câu trả lời trước đó, điều này làm việc cho Swift 4, cả hai HTTP và 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 phải là tên máy chủ và không chứa bất kỳ lược đồ URL nào.

Các vấn đề liên quan