使用带 HTTPS 的 NSURLConnection 发送 POST
Send POST using NSURLConnection with HTTPS
我一直在互联网上搜索有关使用 HTTPS 向我的服务器发送 POST 的正确配置,一些网页说我需要 NSURLCredential
而其他人说:
Just use a @"https://www.myhttpsite.com/" URL and it should work the same way as normal HTTP urls.
那么,怎样才是正确的方法呢?我需要将用户凭据从我的 iOS 应用程序发送到我的服务器以对他们进行身份验证,因此我需要使用 HTTPS 保护此凭据。
我的服务器在使用 Internet 浏览器的 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);
}];
发现这是一个很好的例子,你为什么不试试呢?
NSString *urlstring =[[NSString alloc] initWithFormat:@"userName=%@&password=%@",userName.text,password.text];
NSURL *url=[NSURL URLWithString:@"https://www.example.com/mobile/Login.php"];
NSData *postData = [urlstring dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error;
NSURLResponse *resp;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
我一直在互联网上搜索有关使用 HTTPS 向我的服务器发送 POST 的正确配置,一些网页说我需要 NSURLCredential
而其他人说:
Just use a @"https://www.myhttpsite.com/" URL and it should work the same way as normal HTTP urls.
那么,怎样才是正确的方法呢?我需要将用户凭据从我的 iOS 应用程序发送到我的服务器以对他们进行身份验证,因此我需要使用 HTTPS 保护此凭据。
我的服务器在使用 Internet 浏览器的 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);
}];
发现这是一个很好的例子,你为什么不试试呢?
NSString *urlstring =[[NSString alloc] initWithFormat:@"userName=%@&password=%@",userName.text,password.text];
NSURL *url=[NSURL URLWithString:@"https://www.example.com/mobile/Login.php"];
NSData *postData = [urlstring dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error;
NSURLResponse *resp;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];