Simple http post example in Objective-C?

后端 未结 8 1463
谎友^
谎友^ 2020-11-28 01:32

I have a php webpage that requires a login (userid & password). I have the user enter the information into the app just fine.. but I need an example on how to do a POST

相关标签:
8条回答
  • 2020-11-28 02:23

    I am a beginner in iPhone apps and I still have an issue although I followed the above advices. It looks like POST variables are not received by my server - not sure if it comes from php or objective-c code ...

    the objective-c part (coded following Chris' protocol methodo)

    // Create the request.
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.php"]];
    
    // Specify that it will be a POST request
    request.HTTPMethod = @"POST";
    
    // This is how we set header fields
    [request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    // Convert your data and set your request's HTTPBody property
    NSString *stringData = [NSString stringWithFormat:@"user_name=%@&password=%@", self.userNameField.text , self.passwordTextField.text];
    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = requestBodyData;
    
    // Create url connection and fire request
    //NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSData *response = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:nil error:nil];
    
    NSLog(@"Response: %@",[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
    

    Below the php part :

    if (isset($_POST['user_name'],$_POST['password'])) 
    
    {
    
    // Create connection
    $con2=mysqli_connect($servername, $username, $password, $dbname);
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else 
    {
    // retrieve POST vars
    $username = $_POST['user_name'];
    $password = $_POST['password'];
    
    $sql = "INSERT INTO myTable (user_name, password) VALUES ('$username',   '$password')";
    $retval = mysqli_query( $sql, $con2 );
    if(! $retval )
    {
    die('Could not enter data: ' . mysql_error());
    }
    echo "Entered data successfully\n";
    
    mysqli_close($con2);
    
    }
    }
    else
    {
    echo "No data input in php";
    }
    

    I have been stuck the last days on this one.

    0 讨论(0)
  • 2020-11-28 02:24

    This is what I recently used, and it worked fine for me:

    NSString *post = @"key1=val1&key2=val2";
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://www.nowhere.com/sendFormHere.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    

    Originally taken from http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html, but that blog does not seem to exist anymore.

    0 讨论(0)
提交回复
热议问题