title: NSUrlSession proxy request fails with error code 310 (kCFErrorHTTPSProxyConnectionFailure)
body: <p>I'm trying to send a POST request to a specific URL through a proxy server. To test that the code I'm writing is working, I installed <strong>squidman</strong> on my machine and started a proxy server on port <strong>33074</strong>. I tested the proxy server by changing the network settings to use the proxy when making HTTP/HTTPS requests and it's working ok.</p>
  
  <p>Now I wrote the following code:</p>
  
  <pre class="lang-c prettyprint-override"><code>    NSDictionary* proxyConfig = @{
          (NSString*) kCFNetworkProxiesHTTPSEnable: @(1),
          (NSString*) kCFNetworkProxiesHTTPSProxy: @"127.0.0.1",
          (NSString*) kCFNetworkProxiesHTTPSPort: @"33074",
      };
  
      NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
      [sessionConfig setTimeoutIntervalForRequest:60];
      [sessionConfig setConnectionProxyDictionary:proxyConfig];
  
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
      [request setURL:[NSURL URLWithString:@"https://the-host-where-im-posting"]];
      [request setHTTPMethod:@"POST"];
      [request setValue:[[NSString alloc] initWithFormat:@"%lu",length] forHTTPHeaderField:@"Content-Length"];
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:data];
  
      NSURLSession *session = [self createHttpSession];
      NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
          NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
          if(httpResponse.statusCode == 200){
              //...
          } else {
              LOGERROR(@"Error: %@", error);
          }
      }
  </code></pre>
  
  <p>The problem is that the request is not successfull and the following error is logged:</p>
  
  <blockquote>
    <p>Error: Error Domain=kCFErrorDomainCFNetwork Code=310 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2096, _kCFStreamErrorDomainKey=4}</p>
  </blockquote>
  
  <p><strong>Additional Info:</strong><br>
      1. It should be noted that the requests submits perfectly without setting the <code>proxyConfig</code> dictionary.<br>
      2. A similar error gets reported if I'm trying to submit the request using HTTP through the proxy (by correspondingly changing the keys in the <code>proxyConfig</code> dictionary) with a small diff in the Err Code and _kCFStreamErrorCodeKey: 306 instead of 310 and -2094 instead of -2096.<br>
      3. The process making the request runs as a daemon.</p>
  
  <p>What exactly am I doing wrong ? What am I missing ?</p>