views:

94

answers:

2

Hi,

I have a php webpage that requires a login (userid & password). I have the user enter the information into the app just fine.. but I need an example on how to do a POST command to a website. The apple example on the support site is rather complicated showing a picture upload.. mine should be simpler.. I just want to post 2 lines of text.. Anyone have any good examples?

Alex

+3  A: 

How about

NSString *post = @"key1=val1&key2=val2";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.nowhere.com/sendFormHere.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

Taken from http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html . Thats what I recently used, and it worked fine for me.

Btw, 5 seconds of googling would've let to the same result, using the terms "post request cocoa" ;)

x3ro
Thanks.. I will give that a shot.. I googled for "xcode http post" and "xcode html post" without much luck :)Thanks!
Alex van Es
Thats because, as vikingosekundo already said, xcode has nothing to to with it, its just the IDE you are using. You are programming in Objective-C, using the Cocoa framework.
x3ro
@Alex: You are new to StackOverflow. "Thank you" should be expressed by a upvote
vikingosegundo
ah, you haven't got enough reputation yet to do so...
vikingosegundo
+2  A: 

ASIHTTPRequest makes network communication really easy

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];
vikingosegundo