繁体   English   中英

通过SSL HTTPS从iphone发送POST数据

[英]Sending POST data from iphone over SSL HTTPS

海全,

在我的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);

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

我终于找到了一种通过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);

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


@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

礼貌:

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

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

我订了

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

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

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

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

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

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

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

希望这可以帮助。

要调试HTTP问题,最好的办法是获取Charles HTTP代理应用程序 - 它将通过模拟器记录与服务器之间的所有HTTP通信(如果需要,您甚至可以将其设置为手机的代理) 。

然后,使用程序Curl(来自Terminal,它内置)生成一个工作发布请求(您必须在线搜索使用CURL的示例)。 然后,您只需比较CURL的工作请求的格式,以及您的应用程序发送的内容...请注意,模拟器会自动重定向以通过Charles工作,您必须告诉curl用于发送内容的代理地址通过查尔斯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM