How to retrieve binary data through url?

Web server I am testing returns binary data to the url request.
Is there a way I can save it to the file from script?

If not directly, what is the best way to do this through shell() or appleScript?

If you are already retrieving the data from a URL then it’s very easy to then write that data to a file using the put syntax.

put url ? into 
file "~/" 

Script generates an error when I do that, it only accepts text content.
result() returns 1.

By default it is UTF8, and I have in the past had to set it to the described type in the data coming back… In some cases as simple as

set the defaultStringEncoding to "Western (ISO Latin 1)"

Where the quoted string value was the defined type in the html I was trying to retrieve. Sometimes using a browser (Safari, Opera, whatever) with view source helps here.

Also, you may want to post your error content, that helps to know if there is a panel (post an image shot of it), the return value of 1 is not all that helpful.

The approach you suggested worked! Thanks a lot!