Schlagwort-Archive: nsurlconnection

ASObjC: NSURLConnection – http get and post

--  pgURLAppDelegate.applescript
--  pgURL
--  Created by goodtime on 4/10/11.
--  Copyright 2011 NiceMac. All rights reserved.

-- demonstrates doing a GET or POST Request using NSURLConnection.  Runs in the Background (not in main thread)
-- contains no UI... might add one later, for now it just logs or displays alerts
-- see console log and feel free to change the URL
-- reference material:

script pgURLAppDelegate
    property parent : class "NSObject"
    property self : current application's pgURLAppDelegate -- should match the script delegate name above

    property myURL : ""  -- URL of your request
    property postMethod : "POST"  -- use POST for POST
    property myBody : ""  -- PUT YOUR POST MESSAGE HERE if trying to do a POST

    -- URL Request Encoding
    property myEncoding : NSUTF8StringEncoding of current application --code for UTF8

    property getMethod : "GET"  -- use GET for any other Requests
    property intermediateMsg : "" --  Text String returned from the request (may not be complete for long downloads)
    property entireMsg : "" --  All the Text String returned from the request

    on pgURL_(myURL, myBody, myMethod)
        -- String to NSURL
        tell class "NSURL" of current application
            set myURL to URLWithString_(myURL)
        end tell

        -- String 
        tell class "NSString" of current application
            set myBody to stringWithString_(myBody)

        set myBody to myBody's dataUsingEncoding_(myEncoding)

        tell class "NSString" of current application
            set myMethod to myMethod

        tell NSMutableURLRequest of current application
            set myRequest to requestWithURL_(myURL)

        tell myRequest

        -- add the Message Body when sending a POST Method
        if myMethod = "POST" then
            tell myRequest

        -- form the connection
        set myConnection to (((current application's class "NSURLConnection")'s alloc)'s initWithRequest_delegate_(myRequest, self))

    -- handle the connection in the background (not in main thread)
    -- this is controlled by the next four threads
    on connection_didReceiveResponse_(myConnection, response)

        tell class "NSHTTPURLResponse" of current application
            set statusText to (localizedStringForStatusCode_(statusCode of response)) as text
            set statusCode to (statusCode of response) as string

        -- if it fails to do anything, show what it didn't do here (the error)
        if statustext = "no error" then
            -- I am not really sure if this does anything or if it is reseting the returnData properly
            tell current application's returnData
            display alert "HTTP Error: " & statusCode & return & "Error Message: " & statusText & "." 

    on connection_didReceiveData_(myConnection, returnData)

        -- convert Data returned to String (Don't ever forget how to do this, it is a pain when do forget)
        set my intermediateMsg to (((current application's class "NSString")'s alloc)'s initWithData_encoding_(returnData, current application's NSUTF8StringEncoding)) as string
        log "didReceivedData:"

        log intermediateMsg & return & return

        set my entireMsg to my entireMsg & intermediateMsg


    on connection_didFailWithError_(myConnection,trpErr)
        -- display alert trpErr as string
        log trpErr

        set EM to ""
            set newError to (NSLocalizedDescription of userInfo of (trpErr)) as string
                on error EM
            -- if AppleScript can't do this, so what else is wrong
            set errorMore to EM

        display alert newError & return & return & errorMore

    on connectionDidFinishLoading_(myConnection)
        log "connection Finished."

        -- here you can do what you want to do with the intermediateMsg
        log "here is your entire message returned:"
        log entireMsg

        log "end of line."

    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened 

        -- run the request here for demonstration purposes
        pgURL_(myURL, myBody, getMethod)

    end applicationWillFinishLaunching_

    on applicationShouldTerminate_(sender)
        -- Insert code here to do any housekeeping before your application quits 
        return current application's NSTerminateNow
    end applicationShouldTerminate_

end script