6

我正在设置一个 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];

我会非常感谢你的帮助

4

3 回答 3

7

看看iOS URL 加载系统以及NSURLProtocol

你可以为你的 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 注册到您的委托。

于 2013-08-26T12:14:54.900 回答
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];
于 2013-05-31T02:15:41.440 回答
2

我已将以下代码用于 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){}
于 2016-10-27T15:46:35.977 回答