从iPhone应用程序使用POST发送数据到Web表单 [英] Sending data to web form using POST from iPhone app

查看:135
本文介绍了从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-urlencodedforHTTPHeaderField:@Content-Type];
[request setHTTPBody:postData];

/ *当我们使用https时,我们需要允许任何HTTPS证书,因此添加一行代码,告诉NSURLRequest接受任何https证书,我不确定安全性方面的问题
* /

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

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

NSString * data = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@%@,data);

Web表单的源代码如下。

  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 =插入注册(数字,电子邮件,密码)VALUES(空,
'$ email','$密码');
if(!mysql_query($ sql,$ conn))
{
die('Error:'。mysql_error());
}
}
?>

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



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

解决方案

我用UIWebView解决了这个问题。

调用UIWebView是 -



在头文件中声明

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

在实现文件 -

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

以及这两种方法 -

<$ p $ (无效)webViewDidFinishLoad:(UIWebView *)webView

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


I am trying to send data(a username and password) to an online form from my iPhone app using GET.

This is my source code, but the problem is that no data gets stored in my database.

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

The source code of the web form is below.

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());
  }
}
?>

I have tested the scripts and they work absolutely fine. I am able to store the username and password into a database using an online form. The only issue I am having is to connect to the online form from the app.

I am not getting any error messages in the console or anything unexpected. Just, nothing happens. I will appreciate any help. Thanks

解决方案

I used UIWebView to solve the problem.

Code to call UIWebView is -

In the header file declare

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

In the implementation file -

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

and the two methods -

- (void)webViewDidFinishLoad:(UIWebView *)webView

and

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

这篇关于从iPhone应用程序使用POST发送数据到Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆