在我构建的应用程序中,我需要向API发送安全的POST请求。请求的主体是JSON。指定请求如下:无法通过API向服务器发送https POST请求,获取“HTTPS必需”
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];
这是最常用的,但有时不会,然后会得到一个400:HTTPS Required Message。在服务器端,尽管我已经在URL中指定了端口(甚至有必要这样做),但这些尝试显示为将请求发送到端口80而不是443。
任何想法,我可能会在这里失踪?
编辑: 这是我得到的时候出现这种情况
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";
}
编辑2响应: 思考。
- 是否必须以某种方式配置NSMutableURLRequest以确保它使用安全连接?还是NSURLConnection?
- 为什么它有时会起作用,有时不起作用?
感谢您的回应,但它并没有真正帮我解决我的问题。道歉,如果我的问题不够具体。 – Johanna