从iPhone应用程序使用POST发送数据到Web表单

我正尝试使用GET从我的iPhone应用程序发送数据(用户名和密码)到在线表单。

这是我的源代码,但问题是没有数据存储在我的数据库中。

NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",enterUserName.text,enterPass.text]; NSURL *url=[NSURL URLWithString:@"http://planetimpressions.com/contacts/imagecomm_register.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); 

网页表单的源代码如下。

 if(isset($_GET['submit'])){ $email = $_GET['email']; $password = $_GET['password']; $conn = mysql_connect("mysql", "*****", "*****"); if(!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db("imagecomm_users"); $sql = "INSERT INTO registration(number, email, password) VALUES (null, '$email', '$password')"; if (!mysql_query($sql,$conn)) { die('Error: ' . mysql_error()); } } ?> 

我testing了脚本,他们工作得很好。 我可以使用在线表单将用户名和密码存储到数据库中。 我遇到的唯一问题是从应用程序连接到在线表单。

我没有收到控制台中的任何错误消息或任何意外。 只是,没有任何反应。 我将不胜感激任何帮助。 谢谢

使用ASIHTTP请求来请求/响应服务器。 从这里下载ASIHTTP文件: http ://github.com/pokeb/asi-http-request/tarball/master。 在项目中集成并添加到标题以及在.h文件中定义委托。 现在你可以在你的文件中使用这个代码。

 - (void)requestAuth { NSString* urlString = [NSString stringWithFormat:@" http://planetimpressions.com/contacts/imagecomm_register.php "]; NSURL *url = [NSURL URLWithString:self.urlString]; ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url]; if (!request) { NSLog(@"Request prepared"); return nil; } [request setPostValue: enterUserName.text forKey:@”email”]; [request setPostValue: enterPass.text forKey:@"password"]; [request setTimeOutSeconds:30]; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 [request setShouldContinueWhenAppEntersBackground:YES]; #endif [request setDelegate:self]; [request setDidFailSelector:@selector(ASIHTTPUploadFailed:)]; [request setDidFinishSelector:@selector(ASIHTTPUploadFinished:)]; [request startAsynchronous]; } - (void)ASIHTTPUploadFinished:(ASIHTTPRequest *)theRequest { //Parse coming response NSLog(@"%@",[theRequest responseString]); } - (void)ASIHTTPUploadFailed:(ASIHTTPRequest *)theRequest { //Parse response if request become failure NSLog(@"%@",[theRequest responseString]); } 

我用UIWebView来解决这个问题。

调用UIWebView的代码是 –

在头文件中声明

 UIWebView *myWebView @property(nonatomic, retain) IBOutlet UIWebView ... 

在实现文件中 –

 [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URLTOCALL]]]; 

和两种方法 –

 - (void)webViewDidFinishLoad:(UIWebView *)webView 

 - (void)webViewDidStartLoad:(UIWebView *)webView{