Re: Open safari and send post variables
Re: Open safari and send post variables
- Subject: Re: Open safari and send post variables
- From: Ken Tozier <email@hidden>
- Date: Fri, 9 Jun 2006 13:12:46 -0400
// use the data any way you like
On Jun 9, 2006, at 12:01 PM, Yorh wrote:
Yes true,
But how can I send post variables to my view?
If I use the standard url connection the delegate returns me an
NSData object... and I 'm not able to display the page :(
Is there any way to do it?
I communicate with PHP scripts on a server directly by doing the
following.
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL: <some php script url here>];
NSData *body = <some data here>;
NSString *bodyLength = [[NSNumber
numberWithInt: [body length]] stringValue];
NSHTTPURLResponse *response;
NSError *error;
// generate the HTTP POST
[request setHTTPMethod: @"POST"];
[request setValue: @"application/x-www-form-urlencoded"
forHTTPHeaderField: @"Content-type"];
[request setValue: bodyLength forHTTPHeaderField: @"Content-length"];
[request setValue: @"close" forHTTPHeaderField: @"Connection"];
[request setHTTPBody: body];
// send it away
result = [NSURLConnection sendSynchronousRequest: request
returningResponse: &response
error: &error];
// process result if any
if ((result != nil) && ([result length] > 0))
{
NSString *temp = [[NSString alloc] initWithBytes: [result
bytes]
length: [result length]
encoding: NSUTF8StringEncoding];
NSLog(@"http reply = %@", temp);
}
else if (error != NULL)
{
// something went awry log HTTP header and error
NSLog(@"response = %@", [response allHeaderFields]);
NSLog(@"error = %@", error);
}
In addition, it's fairly easy to serialize Cocoa dictionaries
(although it's too big to post to the list) into php arrays and have
php grab the data and us it directly like this:
$data_from_cocoa_app = unserialize(stripslashes($_POST['query']));
To send it back to you app all you have to do is
echo $result
Serilization helps on this end to convert it to an NSDictionary but
it's too big to post.
If you are interested in the serialization code let me know and I'll
email it to you off-list
Ken
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden