RE: Using the HTTPManager to make a POST

On Thu, 20 Mar 2003, Laird, Brian wrote:

>
> I found another post in the list archive which helped me.  I needed to
> set the content type to be form encoded, and then everything was fine.
>
> server_request.setContentType(MimeType.APPLICATION_X_WWW_FORM_URLENCODED);

It depends on what you send :) but the "usual" thing is indeed to use this
mime type.
You may also find some code snippets in Winie [1] to do PUT (pretty
similar to POST from a HTTPManager point of view).

[1] http://jigsaw.w3.org/Winie/

>
> Brian
>
> >  -----Original Message-----
> > From: 	Laird, Brian
> > Sent:	Thursday, March 20, 2003 2:48 PM
> > To:	'www-jigsaw@w3.org'
> > Subject:	Using the HTTPManager to make a POST
> >
> > I am looking to perform a login to a remote server, so I have written a filter which performs the sign in and then sends the user their session cookies.  The problem I am having is getting the POST data to be sent as a part of the original POST.  According to the packet sniffer I have, the post data is being sent in a separate message as a HTTP Continue, but the backend server replies prior to getting the request formatted as the HTTP Continue.  Below is my code snippet, if someone could let me know what I am doing wrong, I would really appreciate it.
> >
> > Thanks in advance for any help,
> > Brian
> >
> >
> >
> > 	HttpManager manager = HttpManager.getManager();
> >             org.w3c.www.protocol.http.Request server_request = manager.createRequest();
> >             server_request.setMethod(HTTP.POST);
> >             server_request.setURL(new URL(getLoginURL()));
> >             server_request.setAllowUserInteraction(false);
> >             server_request.setCookie(request.getCookie());
> >
> >             // Update the client request fields:
> >             Enumeration e = request.enumerateHeaderDescriptions();
> >             while ( e.hasMoreElements() ) {
> >                 HeaderDescription d = (HeaderDescription) e.nextElement();
> >                 HeaderValue       v = request.getHeaderValue(d);
> >                 if ( v != null )
> >                 server_request.setHeaderValue(d, v);
> >             }
> >
> >             server_request.removeHeader("keep-alive");
> >             // By removing that HOST, we make sure the real Host header gets
> >             // computed by the client side API
> >             server_request.setHeaderValue(org.w3c.www.protocol.http.Request.H_HOST,null);
> >
> >             // build the post data
> >             PipedOutputStream output_stream = new PipedOutputStream();
> >             OutputStreamWriter writer = new OutputStreamWriter(output_stream);
> >             PipedInputStream input_stream = new PipedInputStream(output_stream);
> >             StringBuffer post_data = new StringBuffer();
> >             post_data.append(getUsernameField());
> >             post_data.append("=");
> >             post_data.append(username);
> >             post_data.append("&");
> >             post_data.append(getPasswordField());
> >             post_data.append("=");
> >             post_data.append(getPassword());
> >             writer.write(post_data.toString());
> >             writer.flush();
> > 	// if I don't do this the runRequest never completes
> >             writer.close();
> >             server_request.setOutputStream(input_stream);
> >             Reply reply = manager.runRequest(server_request);
> >             cookieList = reply.getCookie();
> >
>

-- 
Yves Lafon - W3C
"Baroula que barouleras, au tiéu toujou t'entourneras."

Received on Friday, 21 March 2003 06:10:50 UTC