2009-10-15 85 views
26

海所有发送POST数据,从iphone通过SSL HTTPS

在我的iPhone项目,我需要的用户名和密码传递到Web服务器,以前我通过使用GET方法的数据和使用GET格式的URL (如:本地主机:8888 /登录用户名=管理员密码& =密码?),但现在我需要发送这是一个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呼应

+2

如何不工作的代码? – 2009-10-15 10:00:59

+2

是的,它不起作用 – 2009-10-15 10:11:56

+0

我的意思是,它究竟是如何失败?什么是错误?它崩溃了吗?你在哪里看到问题,在服务器或客户端代码中? – 2009-10-15 10:37:32

回答

9

您正在发送的请求为NSASCIIStringEncoding但寻找NSUTF8StringEncoding

我d set

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

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

然而,由于尼古拉指出 - 如果我们知道是什么错误:-)

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 

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

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 

希望这有助于。

+0

不清楚这是否通过HTTPS。 – oberbaum 2012-06-18 18:18:13

+0

@norskben:当代码工作通过HTTP,还没有通过HTTPS – 2012-06-19 07:57:26

+0

是啊,问题问的HTTPS测试这一点。 – oberbaum 2012-06-19 19:29:11

0

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

然后,使用Curl程序(来自终端,它是内置的)来生成工作岗位请求(您必须在线搜索使用CURL的示例)。然后,您只需比较一下CURL的工作请求是如何格式化的,以及您的应用程序发送的内容是什么......请注意,模拟器会自动重新定向以通过Charles工作,您必须告诉curl使用代理地址来发送事件通过查尔斯。

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); 

避免一条警告消息,请加


@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
+0

这个工作完美的我 – 2010-12-15 06:14:48

+1

注:setAllowsAnyHTTPSCertificate是一个私有API。见http://stackoverflow.com/questions/2001565/alternative-method-for-nsurlrequests-private-setallowsanyhttpscertificateforho获取更多信息。 – 2010-12-24 21:03:07

+4

注意,这个代码将让你的应用程序被拒绝,setAllowsAnyHTTPSCertificate:forHost:是私有API – oberbaum 2012-06-18 18:09:43