【发布时间】:2015-01-14 20:46:59
【问题描述】:
我一直在互联网上搜索使用 HTTPS 向我的服务器发送 POST 的正确配置,一些网页说我需要 NSURLCredential 而其他人说:
只需使用@"https://www.myhttpsite.com/" URL,它的工作方式应该与普通 HTTP url 相同。
那么,正确的方法是什么?我需要将用户的凭据从我的 iOS 应用程序发送到我的服务器以对其进行身份验证,因此我需要使用 HTTPS 保护此凭据。
我的服务器已经在使用互联网浏览器的 HTTPS 上运行良好。
到目前为止,我拥有的是这样的:
NSString *user = @"user";
NSString *pass = @"pass";
NSString *postData = [NSString stringWithFormat:@"user=%@&pass=%@", user, pass];
NSURL *url = [NSURL URLWithString:@"https://myserver.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [postData dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"data %@", strData);
}];
【问题讨论】:
-
查看此 SO 帖子-stackoverflow.com/questions/1571336/…
-
@Raghav_1357 我发现了这个:stackoverflow.com/questions/10063224/ios-https-authentication 但这是一个完全不同的解决方案......我应该使用哪一个?
-
我用一些代码发布了一个答案,你为什么不试试呢?
标签: ios post https nsurlconnection