doulian4467 2011-11-07 05:45
浏览 46
已采纳

使用iPhone应用程序中的POST将数据发送到Web表单

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

  • 写回答

2条回答 默认 最新

  • dongwh1992 2011-11-13 11:47
    关注

    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{
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂