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

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败