iOS any body knows how to add a proxy to NSURLRequest?

后端 未结 3 1335
忘了有多久
忘了有多久 2020-12-24 09:57

I\'m setting up a webview but I need to load the content of the webview using a proxy. Any of you knows how can I\'m implement the proxy in NSURLRequest?

for example

3条回答
  •  甜味超标
    2020-12-24 10:38

    I have used the below code for 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){}
    

提交回复
热议问题