Re: Open safari and send post variables
Re: Open safari and send post variables
- Subject: Re: Open safari and send post variables
- From: Yorh <email@hidden>
- Date: Fri, 9 Jun 2006 15:55:16 +0200
Thank you for your answer,
I know what you mean , but there are some problems:
1- In the URL field I will see the tmp path of course and not the
site URL
2- The images of that web site will not loaded because the path...
So I don't think this is the correct way to interact trough cocoa
with a web site...
But if you have some other idea... :)
Thank you so much for your kind support
Yorh
On 09/giu/06, at 14:57, Conor Dearden wrote:
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