2011-11-07 23 views
-1

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

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

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

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 = "INSERT INTO registration(number, email, password) VALUES (null, 
'$email', '$password')"; 
if (!mysql_query($sql,$conn)) 
    { 
    die('Error: ' . mysql_error()); 
    } 
} 
?> 

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

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

+0

是否有错误?那最后的日志显示出什么意外?网络上的数据包跟踪显示了您的期望?您是否调试过服务器以查看它正在接收的内容?你知道数据库连接正确吗?这段代码和你的数据库之间有很大的差距,这段代码片段只涉及它的一小部分。在发布这样的问题之前,请做更多的研究,或者如果您不知道,请问如何做这项研究。 –

+0

编辑了问题以反映您建议的更改 –

回答

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{ 
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]); 
} 
+0

ASIHTTPRequest已停用,建议您不要使用它:http://allseeing-i.com/%5Brequest_release%5D –

相关问题