GET and POST

In an earlier article, we looked at creating a generic "NetOp" script and a specialised "NetOp.Preloader" script that would extend this. In this tutorial, we will create a "NetOp.Transaction" script to send information to (and get responses from) a server using METHOD-GET or METHOD-POST type transactions.

Here is the "NetOp.Transaction" script. If you pass a 'data' parameter (a property list), it will use the POST method. If this parameter is not provided, it assumes that the data is encoded in the URL string and will use GET.

-- "NetOp.transaction" Parent Script (v.1)





property ancestor 

property NetID

property URL, FORMDATA



on new (me, netAddress, data)

  me.URL = netAddress

  me.FORMDATA = data

  ancestor = script("NetOp").rawNew()

  callAncestor(#new, me)

  return me

end



on Start (me)

  if voidP(FORMDATA) then me.NetID = getNetText(me.URL )

  else  me.NetID = postNetText(me.URL , me.FORMDATA)

  aTimerObj = timeout(me.string).new(100, #Timer_CheckProgress, me)

  return me.NetID

end



on Destroy (me)

  netAbort(me.NetID)

  me.NetID = VOID

  callAncestor(#Destroy, me)

end



on Timer_CheckProgress (me, aTimer)

  if voidP(me.NetID) then

    -- we have aborted the operation

    atimer.forget()

    return 0

  end if

  

  finished = netDone(me.NetID)

  if finished then

    errorNum = netError(me.NetID)

    theText = netTextResult(me.NetID)

    if stringP(errorNum) then errorNum =0

    aTimer.forget()

    me.GenerateEventMessage(#NetTransactionComplete, \

[#Text: theText, #Error: errorNum])

  else

    -- check the current status 

    status = getStreamStatus(me.NetID)

    currentState = status.state

    -- before sending the status, work out the portion downloaded

    case (currentState) of 

      "InProgress":

        if status.bytesSoFar > 0 then 

          if status.bytesTotal > 0 then 

            f = MIN(1.0, float(status.bytesSoFar)/status.bytesTotal )

          else f = .50

        else

          f = 0

        end if

      "Complete":

        f = 1.00

      otherwise

        f = 0

    end case

    -- add the fractionDone as a property to the statusList

    status.addProp( #fractiondone, f )

    -- inform the callback object of the current state and the % transferred

    me.GenerateEventMessage(#NetTransactionStatusUpdate, status)

  end if

end

You may notice that the network operation doesn't actually start until you call the start method of the object. The reason for this is that you might want to track all your network operations and only allow 4 (or so) to run concurrently. In this demo movie, every time you click a 'test' button, a new netOp object is created. If there are less than four operations still active, the netOp object is added to a 'queue' list. When an operation is finished, it is removed from the 'active' list and the netop is chosen from the queue and started.

Demo Movie

In the demo movie above, the page that the shockwave movie is transacting with is a simple script that looks like this:

EXAMPLE

if (isset($_REQUEST['q'])) $q = $_REQUEST['q'];

else $q = "NOT SET";

print "Hi, this is the server speaking. The value I received was $q";

If you are running JSP, the following will achieve the same result (try setting the URL in the above example to http://dev3.medialight.com.au/examples/list2.jsp):

JSP EXAMPLE

String q = request.getParameter("q");

if  (q==null) { q = "NOT SET";}

out.print("Hi, this is the server speaking. The value I received was " + q);

And if you're running ASP.NET or mono, the following will achieve the same result.

.NET/Mono C# Example



void Page_Load (object o, EventArgs e) {

  string q = "NOT SET";

  if (Request.Form["q"] != null ) q = Request.Form["q"];

  else if (Request.QueryString["q"] != null) q = Request.QueryString["q"];

  Response.Write("Hi, this is the .NET server speaking. 

  The value I received was " +q);

}



Source Movie

First published 29/06/2005