Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

HELP - Executing a HTTP URL with POST variables ?

TheGameTheGame Posts: 46Registered Users
edited November 2008 in iPhone SDK Development
Is it possible in iphone SDK to execute HTTP URL with POST variables?
Just like in HTML form, in action attribute i have there my HTTP URL and
I have two input components that will add using POST methond in the URL.
If I get it the right URL with POST variables I will get XML data from server.

I really need your help guys. Thanks in advance..
Post edited by TheGame on

Replies

  • TheGameTheGame Posts: 46Registered Users
    edited November 2008
    Dont mind how the XML data get from the server. I have it already.. I need only to execute the HTTP URL with POST variables in my iphone app.

    Thank you so much in advance..
  • dicklacaradicklacara Posts: 123Registered Users
    edited November 2008
    Have a look at the NSURLRequest class in the API docs.

    In particular

    – HTTPBody
    – HTTPMethod

    For example:
    	NSMutableString *httpBodyString;
    	NSURL *url;
            NSMutableString *urlString;
    	
    	httpBodyString=[[NSMutableString alloc] initWithString:@"Name=The Big Bopper&Subject=Hello Baby&MsgBody=...You knooow what I like...Chantilly lace..."];
    	urlString=[[NSMutableString alloc] initWithString:@"http://www.somedomain.com/contactform.php"];
    
    	url=[[NSURL alloc] initWithString:urlString];
    	[urlString release];
    	
    	NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
    	[url release];
    
    	[urlRequest setHTTPMethod:@"POST"];
    	[urlRequest setHTTPBody:[httpBodyString dataUsingEncoding:NSISOLatin1StringEncoding]];
    	[httpBodyString release];
    	
    	NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    	
    	if (!connectionResponse)
    	{
    		NSLog(@"Failed to submit request");
    	}
    	else
    	{
    		NSLog(@"--------- Request submitted ---------");
    		NSLog(@"connection: %@ method: %@, encoded body: %@, body: %a", connectionResponse, [urlRequest HTTPMethod], [urlRequest HTTPBody], httpBodyString);
    		NSLog(@"New connection retain count: %d", [connectionResponse retainCount]);
    		responseData=[[NSMutableData data] retain];
    		NSLog(@"response", responseData);
    	}
    
    

    HTH

    Dick
  • TheGameTheGame Posts: 46Registered Users
    edited November 2008
    dicklacara wrote: »
    Have a look at the NSURLRequest class in the API docs.

    In particular

    – HTTPBody
    – HTTPMethod

    For example:
    	NSMutableString *httpBodyString;
    	NSURL *url;
            NSMutableString *urlString;
    	
    	httpBodyString=[[NSMutableString alloc] initWithString:@"Name=The Big Bopper&Subject=Hello Baby&MsgBody=...You knooow what I like...Chantilly lace..."];
    	urlString=[[NSMutableString alloc] initWithString:@"http://www.somedomain.com/contactform.php"];
    
    	url=[[NSURL alloc] initWithString:urlString];
    	[urlString release];
    	
    	NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
    	[url release];
    
    	[urlRequest setHTTPMethod:@"POST"];
    	[urlRequest setHTTPBody:[httpBodyString dataUsingEncoding:NSISOLatin1StringEncoding]];
    	[httpBodyString release];
    	
    	NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    	
    	if (!connectionResponse)
    	{
    		NSLog(@"Failed to submit request");
    	}
    	else
    	{
    		NSLog(@"--------- Request submitted ---------");
    		NSLog(@"connection: %@ method: %@, encoded body: %@, body: %a", connectionResponse, [urlRequest HTTPMethod], [urlRequest HTTPBody], httpBodyString);
    		NSLog(@"New connection retain count: %d", [connectionResponse retainCount]);
    		responseData=[[NSMutableData data] retain];
    		NSLog(@"response", responseData);
    	}
    
    

    HTH

    Dick


    Thanks for the reply, I will check this out.
  • bikrbikr Posts: 272Registered Users
    edited November 2008
    You could always do it as simple as stringWithContentsOfURL and pass a url with variables in it like
    http://test.com/index.php?test=1&test2=2
    

    Or do it in an xmlparser if you need to parse the results.. Either will do the job..

    If you don't want the data in plain text, you can just send the block w/ method post using the above.
    <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291173496&mt=8" target="_blank">RPSOnline</a> - Online Rock Paper Scissors!!<br />
    *Updates*<br />
    2.0 is "For Sale"<br />
    3.0 is "In Development"
Sign In or Register to comment.