通过SSL HTTPS从iphone发送POST数据

shi*_*eph 27 iphone iphone-sdk-3.0

海全,

在我的iphone项目中,我需要将用户名和密码传递给Web服务器,之前我使用GET方法传递数据并使用带有GET格式的URL(例如:localhost:8888/login?userName = admin&password = password)但是现在我需要将此作为POST数据发送,

任何人都可以帮我找到下面这段代码中的错误吗?

代码我试过..


NSString *post =[NSString stringWithFormat:@"userName=%@&password=%@",userName.text,password.text];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"https://localhost:443/SSLLogin/login.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(data);
Run Code Online (Sandbox Code Playgroud)

此页面将使用php echo返回成功或失败

shi*_*eph 28

我终于找到了一种通过iPhone安全连接发送数据的方法:

NSString *post =[[NSString alloc] initWithFormat:@"userName=%@&password=%@",userName.text,password.text];
NSURL *url=[NSURL URLWithString:@"https://localhost:443/SSLLogin/Login.php"];

NSLog(post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[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];

/* when we user https, we need to allow any HTTPS cerificates, so add the one line code,to tell teh NSURLRequest to accept any https certificate, i'm not sure about the security aspects
*/

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",data);
Run Code Online (Sandbox Code Playgroud)

为了避免发出警告信息,请添加


@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
Run Code Online (Sandbox Code Playgroud)

礼貌:

  1. 我所有的筹码都是流动的朋友和支持者
  2. http://www.cocoadev.com/index.pl?HTTPFileUpload
  3. http://blog.timac.org/?tag=nsurlrequest

  • 注意,此代码将拒绝您的应用,setAllowsAnyHTTPSCertificate:forHost:是一个私有API (4认同)

And*_*iih 9

您正在以NSASCIIStringEncoding发送请求,但正在寻找NSUTF8StringEncoding

我订了

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

和

[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

然而,正如尼古拉指出的那样 - 如果我们知道错误是什么会更容易:-)


Vai*_*idu 8

好吧,这完全不是你问题的答案.但作为替代方案,看看这个代码.我成功地使用它来向服务器发送用户名和密码.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:loginURL]];

//set HTTP Method
[request setHTTPMethod:@"POST"];

//Implement request_body for send request here username and password set into the body.
NSString *request_body = [NSString stringWithFormat:@"Username=%@&Password=%@",[Username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [Password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//set request body into HTTPBody.
[request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]];

//set request url to the NSURLConnection
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


if(theConnection) //get the response and retain it
Run Code Online (Sandbox Code Playgroud)

然后,您可以实现以下委托来检查响应

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.