title: iOS HTTPS requests 101
body: <pre><code>NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
  </code></pre>
  
  <p>Very, very frustrating! I've been pulling my hair for hours with this. I'm using a self-signed certificate on my Linode server. The port is 8000, couldn't get it to work on 443. I don't believe this is the reason though. Here's my code, it's 99% boilerplate:</p>
  
  <pre><code>NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.myserver.com:8000/test.json"]];
  NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
  </code></pre>
  
  <p>At the bottom:</p>
  
  <pre><code>#pragma mark NSURLConnectionDelegate
  
  - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
      NSLog(@"protectionSpace: %@", [protectionSpace authenticationMethod]);
  
      // We only know how to handle NTLM authentication.
      if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodNTLM])
          return YES;
  
      // Explicitly reject ServerTrust. This is occasionally sent by IIS.
      if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
          return NO;
  
      return NO;
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
      [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
      NSLog(@"%@", response);
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
      NSLog(@"%@", data);
  }
  
  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
      NSLog(@"didFailWithError");
      NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
  }
  </code></pre>
  
  <p>OMG HELP!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It worked with this delegate method. I'm receiving the response, but there is a problem.</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
      [[challenge sender] useCredential:[NSURLCredential
                                         credentialWithUser:@"user"
                                         password:@"password"
                                         persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
  
  }
  </code></pre>
  
  <p>The "user" and "password" that I have provided are completely random and aren't checked by the server. <strong>How can I verify the credentials before accepting the connection on my server?</strong></p>
  
  <p>EDIT: I'm running a Node.js server</p>