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

时间:2011-11-07 05:45:52

标签: php iphone ios get nsurlconnection

我正在尝试使用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());
  }
}
?>

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

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

2 个答案:

答案 0 :(得分:1)

对服务器使用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]);
}

答案 1 :(得分:0)

我使用UIWebView来解决问题。

调用UIWebView的代码是 -

在头文件中声明

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

在实施文件中 -

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

和两种方法 -

- (void)webViewDidFinishLoad:(UIWebView *)webView

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