Search code examples
objective-chttpxcode4httprequest

cocoa http post request


hello i know this has been asked and answered many times but i cant seem to understand how it works (im new to iphone dev & objective c in general)

i am trying to make a create user for my app. i have a file create.php on my server that expects to get a _POST variable called id. after it is called it echos "DONE" for success and "EXIT" for failure.

i found some code and altered it (to the best of my knowledge) to fit my needs :

NSMutableString *tmpurl = [NSMutableString stringWithString: @"http://mysite.com/"];
[tmpurl appendString:@"create.php"];
NSURL *url = [NSURL URLWithString:tmpurl];
NSString *uid = @"32478907348920437829078902347804930893741"; // should chenge for randome.
NSString *post = [NSString stringWithFormat:@"id=%@", uid];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

now i don't know how to see what the page echos back or even if it has reached it.

any information would be appreciated and thank you in advance.


Solution

  • You are missing the NSURLConnection. Depending if you want to send this request synchronous or async, you may need to implement the NSURLConnectionDelegate methods.