【问题标题】:iOS any body knows how to add a proxy to NSURLRequest?iOS 任何人都知道如何向 NSURLRequest 添加代理?
【发布时间】:2013-05-31 00:08:34
【问题描述】:

我正在设置 webview,但我需要使用代理加载 webview 的内容。你们中的任何人都知道如何在 NSURLRequest 中实现代理?

例如:

    NSString *location=@"http://google.com";
    NSURL *url=[NSURL URLWithString:location];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
//    some code to set the proxy

    [self.myWebView loadRequest:request];

非常感谢您的帮助

【问题讨论】:

    标签: ios uiwebview proxy nsurlrequest


    【解决方案1】:

    看看iOS URL Loading SystemNSURLProtocol

    你可以为你的 NSURLRequest 编写一个自定义的 NSURLProtocol 类。自定义 NSURLProtocol 可以拦截您的请求并为每个请求添加代理。相关的方法是-(void)startLoading,在这个方法中你可以使用Core-Function,它是iOS中的一个低级api来为每个请求添加代理:

    //"request" is your NSURLRequest
    NSURL *url = [request URL];
    NSString *urlString = [url absoluteString];
    
    CFStringRef urlStringRef = (CFStringRef) urlString;
    CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, urlStringRef, NULL);
    CFStringRef requestMethod = CFSTR("GET");
    
    CFHTTPMessageRef myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, myURL, kCFHTTPVersion1_1);
    
    self.httpMessageRef = CFHTTPMessageCreateCopy(kCFAllocatorDefault, myRequest);
    
    CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, myRequest);
    
    // You can add body, headers.... using core function api, CFNetwork.etc
    
    // below code is to set proxy from code if needs
    NSString *hostKey;
    NSString *portKey;
    if ([[[urlString scheme] lowercaseString] isEqualToString:@"https"]) {
        hostKey = (NSString *)kCFStreamPropertyHTTPSProxyHost;
        portKey = (NSString *)kCFStreamPropertyHTTPSProxyPort;
    } else {
        hostKey = (NSString *)kCFStreamPropertyHTTPProxyHost;
        portKey = (NSString *)kCFStreamPropertyHTTPProxyPort;
    }
    
    //set http or https proxy, change "localhost" to your proxy host, change "5566" to your proxy port 
    NSMutableDictionary *proxyToUse = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"localhost",hostKey,[NSNumber numberWithInt:5566],portKey,nil];
    
    CFReadStreamSetProperty(myReadStream, kCFStreamPropertyHTTPProxy, proxyToUse);
    CFReadStreamOpen(myReadStream);
    

    希望这可以帮助你。

    不要忘记将您的自定义 NSURLProtocol 注册到您的委托。

    【讨论】:

    • 不了解 Objective-C,我尝试实现自定义 NSURLProtocol(在我的代码中我将使用 no-proxy://),但我无法理解以下几点: 1) 为什么是 self.httpMessageRef? 2) [urlString scheme] 应该是 [url scheme],对吗?
    【解决方案2】:

    您不能向 NSURLRequest 添加代理。您将需要使用 ASIHTTPRequest 等第 3 方库。

    // Configure a proxy server manually
    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ignore"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setProxyHost:@"192.168.0.1"];
    [request setProxyPort:3128];
    

    【讨论】:

    • ASIHTTPRequest 不再由开发人员维护。考虑使用 AFNetworking。
    • ASIHTTPRequest 完成了这项工作!谢谢!
    • 如果您深入了解 ASIHTTPRequest,setProxyHostsetProxyPort 是如何工作的,低级代码与我上面的答案几乎相同,ASIHTTPRequest 只是包装了所有这些低级核心功能代码。如果它不再被维护,你为什么不自己写呢:-)。
    【解决方案3】:

    我已将以下代码用于 Http 代理。

    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){}
    
    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多