0

i need to pass some post parameters to the server but the php getting an empty array of $_POST this is my code:

 NSString* user=UserNameField.stringValue;
NSString* pass=PasswordField.stringValue;
NSString* ID=IDField.stringValue;

NSURL *url = [NSURL URLWithString:ENTERANCE];
url = [NSURL URLWithString:@"http://sphere-beauty.com/test.php"];


NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString* post=[NSString stringWithFormat:@"uname=%@&passwd=%@&id=%@",user,pass,ID];

[req setHTTPMethod:@"POST"];
[req setHTTPBody: [post dataUsingEncoding:NSUTF8StringEncoding]];



[[browser mainFrame] loadRequest:req];

can anyone help?

Dima
  • 8,586
  • 4
  • 28
  • 57

2 Answers2

2

This code works for me, sending a simple POST request to a test page.

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *name=@"Freddy Krueger!";
    NSString *encodedName = [name urlencode];
    NSString *postString = [NSString stringWithFormat:@"name=%@", encodedName];
    NSData *data = [postString dataUsingEncoding:NSASCIIStringEncoding];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.1.100/www/posttest.php"]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];

    [web loadRequest:request];

    // Non-ARC
    // [request release];
}

The [name urlencode] calls a urlencode category method on the NSString * that is found here: How do I URL encode a string

web is obviously my UIWebView.

Your code might be failing because you are not URL encoding the parameters.

Community
  • 1
  • 1
craigmj
  • 4,827
  • 2
  • 18
  • 22
1
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"www.aaa.com"]];
[request setHTTPMethod:@"POST"];
NSString *request_body = [NSString stringWithFormat:@"u=aaa&p=aaa"];
[request setHTTPBody:[request_body dataUsingEncoding:NSASCIIStringEncoding]];
NSError *error;
NSURLResponse *response;
NSData *theData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

this is how i normally communicate to the server in a synchronous way...

Obaid Maroof
  • 1,523
  • 2
  • 19
  • 40