↑
Main Page
httpPost
and
setDoOutput()
methods. Additionally, the connection shouldn’t use any cached data, so
setUseCaches()
is given an argument of
false
. Just as with the XML HTTP request object, you must
set the
“Content-Type”
header to the appropriate value using the
setRequestProperty()
method:
function httpPost(sURL, sParams) {
var oURL = new java.net.URL(sURL);
var oConnection = oURL.openConnection();
oConnection.setDoInput(true);
oConnection.setDoOutput(true);
oConnection.setUseCaches(false);
oConnection.setRequestProperty(“Content-Type”,
“application/x-www-form-urlencoded”);
//...
}
After the connection has been set up, it’s possible to get an output stream for the request. It’s on the out-
put stream that you place the parameter string using the
writeBytes()
method. After that, a call to
flush()
sends the data along, and the stream can be closed:
function httpPost(sURL, sParams) {
var oURL = new java.net.URL(sURL);
var oConnection = oURL.openConnection();
oConnection.setDoInput(true);
oConnection.setDoOutput(true);
oConnection.setUseCaches(false);
oConnection.setRequestProperty(“Content-Type”,
“application/x-www-form-urlencoded”);
var oOutput = new java.io.DataOutputStream(oConnection.getOutputStream());
oOutput.writeBytes(sParams);
oOutput.flush();
oOutput.close();
//...
}
The next part of the function gets the input stream for the connection and reads the data in, line-by-line,
similar to the
httpGet()
function. Then, the input stream is closed, and the response text returned as
the function value:
function httpPost(sURL, sParams) {
var oURL = new java.net.URL(sURL);
var oConnection = oURL.openConnection();
oConnection.setDoInput(true);
oConnection.setDoOutput(true);
oConnection.setUseCaches(false);
501
Client-Server Communication
19_579088 ch16.qxd 3/28/05 11:42 AM Page 501
Free JavaScript Editor
Ajax Editor
©
→