Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL GET/POST Request objective-c

I have to send get or post request to localhost:

<?php
 if(@$_GET['option']) {
  echo "You said \"{$_GET['option']}\"";
 }else if(@$_POST['option']) {
  echo "You said \"{$_POST['option']}\"";
 }
?>

ive using this code:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php?option=Hello"]];
 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 NSString *get = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

it works, but one time in code. if ill do it another one time, application has terminate.

Im try to use ASIFormDataRequest:

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:@"http://localhost/wsh/index.php"] autorelease];
[request setPostValue:@"option" forKey:@"myFormField1"];
[request start];
NSError *error = [request error];
if (!error) {
  NSString *response = [request responseString];
  NSLog(response);
}else{
  NSLog(@"error");
}

it says:

2010-01-07 13:20:34.964 WSH[3351:903] -[NSCFString absoluteURL]: unrecognized selector sent to instance 0x160f8
2010-01-07 13:20:34.966 WSH[3351:903] error

sry for my english

like image 802
kosmaks Avatar asked Dec 09 '22 17:12

kosmaks


2 Answers

You are using a plain NSString literal where an NSURL object is expected: [...] initWithURL:@"http://localhost/wsh/index.php" [...]

Change this to initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"].

like image 81
Nikolai Ruhe Avatar answered Dec 29 '22 02:12

Nikolai Ruhe


I wonder if also you should switch the value and key for the post values, ie change the line

[request setPostValue:@"option" forKey:@"myFormField1"];

to

[request setPostValue:@"myFormField1" forKey:@"option"];
like image 27
epatel Avatar answered Dec 29 '22 02:12

epatel