In the app I'm building, I need to send a secure POST request to an API. The body of the request is JSON. Specifying the request as below:
NSMutableURLRequest *urlRequest=[NSMutableURLRequest
requestWithURL:
[NSURL URLWithString: @"https://testserver.test:443/userinfo"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue:delegate.currentUser.token forHTTPHeaderField:@".ASPXAUTH"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:postData];
NSURLConnection* connection = [[NSURLConnection alloc]
initWithRequest:urlRequest delegate:self];
This most often works, but sometimes doesn't and will then get a 400: HTTPS Required message back. On the server side, these attempts show as if the request is sent to port 80 rather than 443 even though I've specified the port in the URL (should this even be necessary?).
Any ideas what I could be missing here?
Edit: This is the response I get when this happens
headers: {
"Cache-Control" = "no-cache";
"Content-Length" = 14;
"Content-Type" = "text/plain; charset=utf-8";
Date = "Tue, 16 Apr 2013 12:31:02 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/7.5";
"Set-Cookie" = "ARRAffinity=ed9f________________________________8ac4;Path=/;Domain=testserver.test:443, WAWebSiteSID=c390_________________0; Path=/; HttpOnly";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET, ARR/2.5, ASP.NET";
}
Edit 2: Thoughts.
- Do I have to configure the NSMutableURLRequest somehow to make sure it uses a secure connection? Or the NSURLConnection?
- Why is it that it sometimes works and sometimes not?