Reputation: 351
First time using POST
POST information is not getting from the iPhone to the PhP script. I get no errors anywhere. var_dump($_POST) shows empty. failed login returned. Any ideas where to look next or do I have a visible problem here:
NSURL *url = [NSURL URLWithString:link];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *requestBodyString = [NSString stringWithFormat:@"txtuid=%@&txtpwd=%@", userName , password];
NSData *requestBody = [NSData dataWithBytes:[requestBodyString UTF8String]length:[requestBodyString length]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestBody];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; // added from first suggestion
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
After trying it six different ways, I took a modified version from a previous post.
And yet another attempt with the same results.
NSData *data = [[NSString stringWithFormat: @"txtuid=%@&txtpwd=%@", userName , password] dataUsingEncoding:NSUTF8StringEncoding];
// set up request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:link]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"--------------------------------------------------"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// body
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
Upvotes: 1
Views: 825
Reputation: 6593
Use following code. make sure that post string's receipt is a key and will be used in server. client side code
NSString *receipt1 = @"username";
NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1];
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://localhost:8888/validateaction.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
{
NSLog(@"Response: %@", result);
}
}
server side php script.
validation.php
<?php
if(_POST)
{
if($_POST['receipt'] == 'username')
{
echo "post successfull";
$receipt = $_POST['key1'];
echo $receipt;
}
else
{
echo "not post";
}
?>
Upvotes: 0
Reputation: 50697
See my answer here about how to use POST to upload an image. It posts to forms in a PHP file. Same concept for posting data and not uploading images, but this is a working example of how to post to a PHP script using Objective-C.
Upvotes: 2
Reputation: 181280
Try not setting the content-type
explicitly. Avoid the following line:
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
If you are using the iPhone simulator, I would strongly recommend you using a PROXY for HTTP debugging purposes. mitmproxy
is awesome. And very easy to setup on Mac OS X.
Upvotes: 0