- From: Laird, Brian <BLaird@perseco.com>
- Date: Tue, 25 Mar 2003 17:10:57 -0600
- To: "Yves Lafon" <ylafon@w3.org>
- Cc: <www-jigsaw@w3.org>
Yves,
Thanks for the followup. I had a few questions from the code in Winie. There is code to flush a reply of its contents so that it would be used by pool again. Is that truly needed based on what I am doing below (see first email I had sent)? Another follow up question, do I need to do anything to return the reply and/or request to the HttpManager for pooling purposes?
Thanks for the continued help,
Brian
(Code from JW.java)
/**
* Should be called to purge the reply, otherwise the connection won't
* be marked idle and won't be reused.
* @param reply the reply to purge.
*/
protected void skipBody(Reply reply) {
try {
if (reply.hasInputStream()) {
InputStream in = reply.getInputStream();
byte buffer[] = new byte[256];
try {
while (in.read(buffer) != -1);
} catch (IOException ioex) {
try { in.close(); } catch (IOException ioex2) {}
}
}
} catch (IOException ex) {
//nothing to do
}
}
-----Original Message-----
From: Yves Lafon [mailto:ylafon@w3.org]
Sent: Friday, March 21, 2003 5:11 AM
To: Laird, Brian
Cc: www-jigsaw@w3.org
Subject: 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 Tuesday, 25 March 2003 18:11:10 UTC