Re: Open safari and send post variables
Re: Open safari and send post variables
- Subject: Re: Open safari and send post variables
- From: Conor Dearden <email@hidden>
- Date: Fri, 09 Jun 2006 14:57:46 +0200
> Your code of course works... but I need to open safari and print the variables
> I sent (with php).
> I know how to do it with php, but i write:
What is it exactly your trying to achieve? From your post I am guessing you
want to open the resulting page from the post in Safari. Do the post with
NSURLConnection as mentioned before. Gather all the data that comes back,
when the connection is done loading, write the data to a temp file on disk
and load that file into Safari with openURL:
Here is the code: theConnection, receivedData are instance variables. You
have to add to your header.
- (void)startPost {
NSURL *someUrl = [NSURL URLWithString:@"http://www.test.org/test.php"];
NSMutableURLRequest* postURLRequest = [NSMutableURLRequest requestWithURL:
someUrl];
//Make it a post
[postURLRequest setHTTPMethod:@"POST"];
//Set the body for the post
[postURLRequest setHTTPBody:[@"test=foo"
dataUsingEncoding:NSISOLatin1StringEncoding]];
//Start the connection with the post
theConnection=[[NSURLConnection alloc] initWithRequest:postURLRequest
delegate:self];
if (theConnection) {
receivedData=[[NSMutableData data] retain];
}
}
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
//redirects and such
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data
{
// append the new data to the receivedData
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
[receivedData release];
receivedData = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//get a temp file path
NSString *tempPath = [NSString stringWithFormat:@"%@/myFile.html",
NSTemporaryDirectory()];
//write the data out
[receivedData writeToFile:tempPath options:nil error:nil];
//laod it in safari
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:tempPath]];
/*
//if you want to debug the htmlsource code you can use this functions
NSString *htmlSource = [[[NSString alloc] initWithData:receivedData
encoding:NSISOLatin1StringEncoding] autorelease];
NSLog(@"%@", htmlSource);
*/
// release the connection, and the data object
[connection release];
[receivedData release];
receivedData = nil;
}
Conor
http://www.bruji.com/
_______________________________________________
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