RE: Using the HTTPManager to make a POST
Yves Lafon <[email protected]> Fri, 21 Mar 2003 12:10:46 +0100 (MET)
| Newsgroups | gmane.comp.web.jigsaw |
|---|---|
| Message-ID | <[email protected]> |
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_URLENCODE= D); It depends on what you send :) but the "usual" thing is indeed to use thi= s 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: '[email protected]' > > 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 sessio= n cookies. The problem I am having is getting the POST data to be sent a= s a part of the original POST. According to the packet sniffer I have, t= he 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 wh= at I am doing wrong, I would really appreciate it. > > > > Thanks in advance for any help, > > Brian > > > > > > > > HttpManager manager =3D HttpManager.getManager(); > > org.w3c.www.protocol.http.Request server_request =3D mana= ger.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 =3D request.enumerateHeaderDescriptions(); > > while ( e.hasMoreElements() ) { > > HeaderDescription d =3D (HeaderDescription) e.nextEle= ment(); > > HeaderValue v =3D request.getHeaderValue(d); > > if ( v !=3D null ) > > server_request.setHeaderValue(d, v); > > } > > > > server_request.removeHeader("keep-alive"); > > // By removing that HOST, we make sure the real Host head= er gets > > // computed by the client side API > > server_request.setHeaderValue(org.w3c.www.protocol.http.R= equest.H_HOST,null); > > > > // build the post data > > PipedOutputStream output_stream =3D new PipedOutputStream= (); > > OutputStreamWriter writer =3D new OutputStreamWriter(outp= ut_stream); > > PipedInputStream input_stream =3D new PipedInputStream(ou= tput_stream); > > StringBuffer post_data =3D new StringBuffer(); > > post_data.append(getUsernameField()); > > post_data.append("=3D"); > > post_data.append(username); > > post_data.append("&"); > > post_data.append(getPasswordField()); > > post_data.append("=3D"); > > 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 =3D manager.runRequest(server_request); > > cookieList =3D reply.getCookie(); > > > --=20 Yves Lafon - W3C "Baroula que barouleras, au ti=E9u toujou t'entourneras."