I am updating an old app, and my old code which uploads POST messages to a PHP script is not working. It seems like the POST array is simply empty. I've looked online and my code looks just like all the examples I see.
just tried the answer given here, a simple example, and I get nothing in the POST array:
https://stackoverflow.com/questions/28654603/php-post-script-works-with-other-sites-but-not-with-ios-nsurlconnection
Here is the code from that example that does not work for me:
@implementation Test : NSObject
-(void)userLogin {
NSString *user = @"test";
NSString *pass = @"pass";
NSString *post = [NSString stringWithFormat: @"user=%@&pass=%@", user, pass];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:@"www.example.com/login.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
sleep(50);
NSLog(@"%@", conn);
}
@end
--------------- PHP file -----------------------
<?php
file_put_contents('/tmp/test', print_r($_POST, true));
?>
On the other hand, if I go to a site like this and send a message with a simple 'user=me' and 'pass=whatever', then my php script does show that it received the variables.
https://reqbin.com/post-online
Furthermore, if I go to a site that will receive POSTs, like this one, then the example code above works fine.
http://ptsv2.com
Anyone know what could be the issue?