Re: curl equivalence
Re: curl equivalence
- Subject: Re: curl equivalence
- From: Christopher Nebel <email@hidden>
- Date: Thu, 27 Oct 2005 17:03:32 -0700
On Oct 27, 2005, at 1:32 PM, John R. wrote:
On 26 Oct 2005 11:56:08 Christopher Nebel said
If you don't mind using Objective-C, then you can get essentially
ideal results. Essentially, you want to use one of the NSURL
family -- these are tied into the same foundation that Safari
uses, so you get the same cookie jar. Depending on the API you
use, you can get different effects, but the one that's the
simplest and most "correct" (i.e., requires the least post-
processing of the answer) is + [NSURLConnection
sendSynchronousRequest:response:error:]. You do have to check the
response status code yourself, though.
Is it possible to write an Objective-C handler that can be called
from Applescript developed on Script Editor? I have not yet used
Applescript Studio or Xcode, through which I suppose integration
with Objective-C is easier. Maybe this is an excuse to try it.
I think it's possible if you're sufficiently clever, but it's not
really designed for it. Studio is.
What I want to do should be simple in theory, right? -- load a url
and loop "to check the response status code"? Please point me to
the documentation on this API +[NSURLConnection
sendSynchronousRequest:response:error:], and what the "foundation"
classes are for Safari.
Documentation for that specific method is at <http://
developer.apple.com/documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSURLConnection.html#//apple_ref/doc/uid/
20001697-BAJHAHGI>; there's more general documentation at <http://
developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/
index.html>.
No loop is necessary -- that's what the "synchronous" in the method
name means. You just need to check the response code manually, since
a failed HTTP request won't be reported as an error. Here's the code
I used:
NSURL *u = [NSURL URLWithString:url];
NSURLRequest *q = [NSURLRequest requestWithURL:u];
NSURLResponse *r = nil;
NSData *d = nil;
NSError *e = nil;
int status;
d = [NSURLConnection sendSynchronousRequest:q
returningResponse:&r error:&e];
status = [r isKindOfClass:[NSHTTPURLResponse class]] ?
[(NSHTTPURLResponse *) r statusCode] : 200;
if (!d || e || status < 200 || status > 299) {
/* an error! */
}
else {
/* d holds the page data, proceed accordingly. */
}
--Chris Nebel
AppleScript and Automator Engineering
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden