Java Reference
In-Depth Information
How it works...
The HttpRequest object works in similar ways when using the POST method as when
using the GET method: you set up the HttpRequest.location property, then you call the
HttpRequest.start() function to start the interaction between your application and the
server. However, when posting, the data must be properly encoded in ways understood by
the HTTP server. Let's take a look at the function postData() :
F Capture data in pair sequence —each data item from the form to be sent to the server
must be grouped in key/value pairs, where the key is the name of a field in an HTTP
form and the value is from the TextBox instances. To accomplish this, we place the
data in a sequence of Pair instances. A Pair object is a tuple, where you can store
name-value pairs. We declare variable postData as a sequence of Pair instances
to store values for firstName , lastName , title , and address , respectively, as
shown next:
var postData = [
Pair{name:"firstName" value:(scene.lookup("fName") as
TextBox).text},
Pair{name:"lastName" value:{(scene.lookup("lName") as
TextBox).text}},
Pair{name:"title" value:(scene.lookup("title") as
TextBox).text},
Pair{name:"address" value:(scene.lookup("addr") as
TextBox).text},
];
F Sending data to server —to communicate with the server, we instantiate
HttpRequest and assign it to variable http . Next, the code sets the parameter
location to point to the server that will receive the data. The code also sets
the method property to HttpRequest.POST (instead of GET ).
The code defines the event-handler function onOutput:function
(:OutputStream) as a callback function that provides access to
an instance of OutputStream assigned to variable toServer . All
values written to that stream will be sent to the server.
Before data from the form can be posted to the server, the
sequence of Pair instances must be converted into a format
supported by the web server. The code uses function encodePa
rameters(Pair[]):String from the URLConverter class to
encode the values that will be sent to the server.
 
Search WWH ::




Custom Search