Using NSURLRequest to pass key-value pairs to PHP script with POST

后端 未结 6 2075
灰色年华
灰色年华 2021-01-31 20:51

I\'m fairly new to objective-c, and am looking to pass a number of key-value pairs to a PHP script using POST. I\'m using the following code but the data just doesn\'t seem to b

6条回答
  •  别那么骄傲
    2021-01-31 21:03

    Thanks for the suggestions everyone. In the end i managed to solve the issue by using stuff given here.

    Code:

    NSString *myRequestString = @"sender=my%20sender&rcpt=my%20rcpt&message=hello";
    NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
    NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://people.bath.ac.uk/trs22/insert.php" ] ]; 
    [ request setHTTPMethod: @"POST" ];
    [ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [ request setHTTPBody: myRequestData ];
    NSURLResponse *response;
    NSError *err;
    NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
    NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
    NSLog(@"responseData: %@", content);
    

提交回复
热议问题