使用自定义 NSURLProtocol 和 HTTP 代理处理重定向
Handling redirects with custom NSURLProtocol and HTTP proxy
我有一个自定义 URLProtocol,我想通过代理服务器重定向所有流量。
我当前的工作代码如下所示:
+(BOOL)canInitWithRequest:(NSURLRequest*)request
{
if ([NSURLProtocol propertyForKey:protocolKey inRequest:request])
return NO;
NSString *scheme = request.URL.scheme.lowercaseString;
return [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"];
}
-(void)startLoading
{
NSMutableURLRequest *request = self.request.mutableCopy;
[NSURLProtocol setProperty:@YES forKey:protocolKey inRequest:request];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
config.connectionProxyDictionary = @
{
(id)kCFNetworkProxiesHTTPEnable:@YES,
(id)kCFNetworkProxiesHTTPProxy:@"1.2.3.4",
(id)kCFNetworkProxiesHTTPPort:@8080
};
m_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];
[[m_session dataTaskWithRequest:request] resume];
}
目前效果很好。问题是有一些 url 使用重定向 - 我希望重定向也由代理服务器执行,而不是由设备执行。我尝试添加以下代码,但没有帮助:
-(void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task willPerformHTTPRedirection:(NSHTTPURLResponse*)response newRequest:(NSURLRequest*)newRequest completionHandler:(void (^)(NSURLRequest*))completionHandler
{
NSMutableURLRequest *request = newRequest.mutableCopy;
[NSURLProtocol removePropertyForKey:protocolKey inRequest:request];
[self.client URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
[task cancel];
[self.client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]];
}
问题是新请求没有被发送到代理服务器,而是被设备本身重定向。
谢谢。
原来问题出在 HTTPS 服务器的重定向上,而没有定义 HTTPS 代理。要使用 HTTPS 代理,代码应如下所示:
config.connectionProxyDictionary = @
{
@"HTTPEnable":@YES,
(id)kCFStreamPropertyHTTPProxyHost:@"1.2.3.4",
(id)kCFStreamPropertyHTTPProxyPort:@8080,
@"HTTPSEnable":@YES,
(id)kCFStreamPropertyHTTPSProxyHost:@"1.2.3.4",
(id)kCFStreamPropertyHTTPSProxyPort:@8080
};
来源:
我有一个自定义 URLProtocol,我想通过代理服务器重定向所有流量。
我当前的工作代码如下所示:
+(BOOL)canInitWithRequest:(NSURLRequest*)request
{
if ([NSURLProtocol propertyForKey:protocolKey inRequest:request])
return NO;
NSString *scheme = request.URL.scheme.lowercaseString;
return [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"];
}
-(void)startLoading
{
NSMutableURLRequest *request = self.request.mutableCopy;
[NSURLProtocol setProperty:@YES forKey:protocolKey inRequest:request];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
config.connectionProxyDictionary = @
{
(id)kCFNetworkProxiesHTTPEnable:@YES,
(id)kCFNetworkProxiesHTTPProxy:@"1.2.3.4",
(id)kCFNetworkProxiesHTTPPort:@8080
};
m_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];
[[m_session dataTaskWithRequest:request] resume];
}
目前效果很好。问题是有一些 url 使用重定向 - 我希望重定向也由代理服务器执行,而不是由设备执行。我尝试添加以下代码,但没有帮助:
-(void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task willPerformHTTPRedirection:(NSHTTPURLResponse*)response newRequest:(NSURLRequest*)newRequest completionHandler:(void (^)(NSURLRequest*))completionHandler
{
NSMutableURLRequest *request = newRequest.mutableCopy;
[NSURLProtocol removePropertyForKey:protocolKey inRequest:request];
[self.client URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
[task cancel];
[self.client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]];
}
问题是新请求没有被发送到代理服务器,而是被设备本身重定向。
谢谢。
原来问题出在 HTTPS 服务器的重定向上,而没有定义 HTTPS 代理。要使用 HTTPS 代理,代码应如下所示:
config.connectionProxyDictionary = @
{
@"HTTPEnable":@YES,
(id)kCFStreamPropertyHTTPProxyHost:@"1.2.3.4",
(id)kCFStreamPropertyHTTPProxyPort:@8080,
@"HTTPSEnable":@YES,
(id)kCFStreamPropertyHTTPSProxyHost:@"1.2.3.4",
(id)kCFStreamPropertyHTTPSProxyPort:@8080
};
来源: