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

使用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{
    
    已采纳该答案
    打赏 评论
  • dongzhang0243 2011-11-07 06:07

    Use ASIHTTP request for request/response with server. Download the ASIHTTP file from here: http://github.com/pokeb/asi-http-request/tarball/master. Integrate inside project and add to header as well as define delegate in .h file. Now you can use this code inside your file.

    - (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]);
    }
    
    打赏 评论

相关推荐 更多相似问题