Sending POST request to Cougaar Servlet
Todd Wright
twright at bbn.com
Tue Mar 11 18:13:00 EST 2003
This is due to the way HTTP POST redirects work, and you'd see this in Tomcat
if your servlet required redirection:
Cougaar Developers' Guide 10.2 page 105 (section 5.2 bullet 8):
8. Here are some important notes on the use "GET v.s. POST v.s. PUT"
as the HTTP request method:
HTML-generating servlets should only use "doGet(..)", where HTML
"<a hrefs=..>" are the same as before, but HTML FORMs should look
like:
<form method="GET" .. /> not POST!
... various "input" and "select" statements
</form>
Java clients that simply use URL-parameters can without writing to
the servlet can remain as-is, since HTTP GET works fine. Java
clients that need to upload data to the servlet, such as
serialized Java Objects or a streamed 'dialog', should use PUT
instead of the default POST method:
UrlConnection uc = ...;
((HttpUrlConnection) uc).setRequestMethod("PUT");
OutputStream os = uc.getOutputStream();
... // usual "upload-data" code
and the servlet should support the "doPut(..)" method. Note that
the UrlConnection's setting of the "GET v.s. POST" method is not
well documented in the Sun javadocs.
The reason for the above "use GET & use PUT instead of POST"
nonsense is that POST requests will not be redirected between
nodes (typically the POST data is lost), whereas GET and PUT will
be correctly redirected. The exact redirection rules are in the
HTTP specification and built into the browser/URLConnection code
itself:
HTTP/1.1 spec on request methods:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
Further notes on POST redirection:
http://ppewww.ph.gla.ac.uk/~flavell/www/post-redirect.html
POST works fine if you find the specify the *correct* host:port in the URL
(i.e. zero redirects are needed). This is awkward if you have a multi-node
society.
I haven't tried this, but I think you can turn off automatic
HttpURLConnection redirection and do your own redirect if necessary. Set the
HttpURLConnection's "setInstanceFollowRedirects(false)", then check the
HttpURLConnection's response code. If the response is in the 300's (e.g.
302), then your POST data was dropped and the HTTP response should include a
"Location:" header with the redirect URL. Then make a new connection to the
redirect's URL and try the POST again.
You'll still need to handle the case where the agent *just* moved, in which
case the POST parameters will be lost again. This is usually better than
having the UI attempt to cache these redirect URLs.
If there's lots of POST data then you may want to use an explicit "check for
redirect" HTTP GET request before attempting the POST.
Todd
On Tuesday 11 March 2003 16:54, Stute, Nick J. wrote:
> I am trying to programmatically send a POST request to a Servlet running on
> one of my agents. I cannot seem to get the parameters to the Servlet. The
> sample code below works fine for a similar servlet running in Tomcat (not
> in Cougaar).
>
> I successfully connect to the servlet, but the parameters don't seem to
> make it.
>
> Here is the code that accesses the Servlet:
>
> URL url = new URL( "http", "localhost", 8800, "/TestServlet");
> URLConnection connection = url.openConnection();
> ((HttpURLConnection)connection).setRequestMethod( "POST");
> connection.setDoInput( true);
> connection.setDoOutput( true);
> OutputStreamWriter out = new OutputStreamWriter(
> connection.getOutputStream());
> out.write( "MODE=AIRCRAFTASSETS&OUTPUT=HTML");
> out.write( "\r\n");
> out.flush();
> out.close();
> BufferedReader br = new BufferedReader( new InputStreamReader(
> connection.getInputStream()));
> while ( br.ready()) {
> System.out.println( br.readLine());
> }
>
> Nick Stute
> Northrop Grumman Information Technology, DES
> Email: nstute at northropgrumman.com
> Phone: 937.431.5918
> Fax: 937.426.8888
>
>
>
> ---------------------------------------------------------------------------
> To unsubscribe from this list please send a message to
> majordomo at cougaar.org with the line "unsubscribe cougaar-developers" in the
> BODY of the message.
---------------------------------------------------------------------------
To unsubscribe from this list please send a message to majordomo at cougaar.org
with the line "unsubscribe cougaar-developers" in the BODY of the message.
More information about the Cougaar-developers
mailing list