IOS does any authority know how to add a proxy server to NSURLRequest?

I am setting up webview, but I need to load the contents of the webpage using a proxy. Do any of you know how I can implement a proxy server in NSURLRequest?

eg:

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

I will be very grateful for your help.

+6
source share
3 answers

Check out the iOS URL loading system as well as NSURLProtocol

You can write your own NSURLProtocol class for your NSURLRequest. Custom NSURLProtocol can intercept your requests and add proxies for each request. the corresponding method -(void)startLoading , inside this method you can use Core-Function, which is a slightly low-level api in iOS to add a proxy to each request:

 //"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); 

Hope this can help you.

Forget registering your custom NSURLProtocol for your delegate.

+7
source

You cannot add a proxy server to NSURLRequest. You will need to use a third-party library, such as ASIHTTPRequest .

 // 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]; 
+2
source

I used the code below for an Http proxy.

 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){} 
+1
source

Source: https://habr.com/ru/post/946223/


All Articles