You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Sebastian Hauer <ha...@psicode.com> on 2003/05/17 18:45:07 UTC

How to get POST request OutputStream?

Hi,

I am kind of stuck here.  From the API and browsing through the code I
don't seem to find a convenient way of obtaining an OutputStream.  I
need this in order to rewrite some code that is currently using
Sun's HttpURLConnection that is doing something like this:

----------------------------------------------
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
OutputStream os = conn.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(someObject);
oos.flush();
----------------------------------------------

I will continue to look through the code but I assume I am missing
something.  Any pointer in the right direction will be appreciated.

Regards,
Sebastian


Re: How to get POST request OutputStream?

Posted by Oleg Kalnichevski <o....@dplanet.ch>.
Have a look at EntityEnclosingMethod class and at
EntityEnclosingMethod#setRequestBody(InputStream) in particular

PostMethod & PutMethod are sub classes of EntityEnclosingMethod

Hope this helps

Oleg




On Sat, 2003-05-17 at 18:45, Sebastian Hauer wrote:
> Hi,
> 
> I am kind of stuck here.  From the API and browsing through the code I
> don't seem to find a convenient way of obtaining an OutputStream.  I
> need this in order to rewrite some code that is currently using
> Sun's HttpURLConnection that is doing something like this:
> 
> ----------------------------------------------
> HttpURLConnection conn = (HttpURLConnection) url.openConnection();
> conn.setRequestMethod("POST");
> OutputStream os = conn.getOutputStream();
> ObjectOutputStream oos = new ObjectOutputStream(os);
> oos.writeObject(someObject);
> oos.flush();
> ----------------------------------------------
> 
> I will continue to look through the code but I assume I am missing
> something.  Any pointer in the right direction will be appreciated.
> 
> Regards,
> Sebastian
> 


Re: How to get POST request OutputStream?

Posted by Oleg Kalnichevski <o....@dplanet.ch>.
Sebastian,

No offense taken. We are perfectly aware of the current design flaws and
limitations. The existing API predates most of the current HttpClient
developers (if not all). We just ended up inheriting the existing API
from the previous generation of the HttpClient developers, and quite
frankly may not know the rationale behind certain design decisions. 

However, the main problem being that historically HttpClient 2.0 was
slow in coming and many folks have come to rely on HttpClient CVS
snapshots in their projects. That forced us into preserving the API that
was not envisaged to be the final one, and was known to be sub-optimal,
as massive API overhaul might have frustrated quite a few of our users. 

Our plan is to press ahead with 2.0 release retaining the existing API
in order to give all our exisiting users a stable release they can rely
upon. Once 2.0 is out of the door, we will finally be able to
concentrate on developing a better, cleaner designed HttpClient for the
next generation of the users. 

Just a few highlights of what we have been thinking of: 1.) currently
HttpClientBase class is a real monstrosity. There's a consensus among us
that redirect & authentication logic needs to be pulled out of the
HttpMethodBase and moved into HttpClient 2.) I personally would like to
see monolithic HttpMethod interface split into HttpRequest/HttpResponse
interface pair. That would greatly simplify the overall design and make
it more flexible in many ways. 

So, stay tuned. Good things will be coming. We just need to finally
agree on the level of imperfection we all are prepared to live with for
the 2.0 release, get it done, and finally move on

Cheers

Oleg  



On Sun, 2003-05-18 at 01:20, Sebastian Hauer wrote:
> Hi Michael and Oleg,
> 
> On Sat, May 17, 2003 at 01:07:54PM -0400, Michael Becke wrote:
> > As Oleg has said HttpClient uses an input stream for getting content 
> > instead of a providing an output stream.  To emulate HttpURLConnection 
> > I can think of a few options.
> > 
> > 1) Buffer content to a file or byte array using something like:
> > 
> > .... 
> > 2) Use a PipedOutputStream.  Take a look at 
> > http://java.sun.com/j2se/1.3/docs/api/java/io/PipedOutputStream.html.  
> > Be sure to note that the PipedInputStream/OutputStream combo will have 
> > to be used from different threads.
> 
> I thought of these options myself but to be honest both seem to be
> too cumbersome.  I don't know how big the serialized objects might
> be I want to send over the wire and therefore I don't want to
> buffer the whole thing and I don't want to spawn a new thread if I
> don't have to.
> 
> I came up with a third idea and wrote a rough implementation.
> Based on the command pattern I wrote a ControlledPostMethod (mabye
> not be best name :) which extends EntityEnclosingMethod and
> overwrote writeRequestBody(HttpState, HttpConnection).
> ControlledPostMethod has a setRequestCommand(RequestCommand) method.
> The passed in RequestCommand has an executeRequest(OutputStream) method that
> gets executed in ControlledPostMethod#writeRequestBody().
> That way I could write to the requests output stream in my custom
> request command and had real streaming.  Nothing need to be buffered
> and no thread was spawned.
> The implementation is not cleaned up otherwise I would have posted
> it here.
> 
> Though after I was done I realized that I don't realy like my own
> implementation.  Obviously as I am new to HTTPClient and on this
> mailing list I don't know the history for the current API design.
> But it appears to me as if the "all-in-one" execute() method that
> will send the request out on its way as well as retrieve the result,
> leaves one with less control over what is going on during the
> request.  I know this is a mouthful to say and I hope no one will be
> offended by me asking this.  Any thoughts?
> 
> Regards,
> Sebastian


Re: How to get POST request OutputStream?

Posted by Sebastian Hauer <ha...@psicode.com>.
Hi Michael and Oleg,

On Sat, May 17, 2003 at 01:07:54PM -0400, Michael Becke wrote:
> As Oleg has said HttpClient uses an input stream for getting content 
> instead of a providing an output stream.  To emulate HttpURLConnection 
> I can think of a few options.
> 
> 1) Buffer content to a file or byte array using something like:
> 
> .... 
> 2) Use a PipedOutputStream.  Take a look at 
> http://java.sun.com/j2se/1.3/docs/api/java/io/PipedOutputStream.html.  
> Be sure to note that the PipedInputStream/OutputStream combo will have 
> to be used from different threads.

I thought of these options myself but to be honest both seem to be
too cumbersome.  I don't know how big the serialized objects might
be I want to send over the wire and therefore I don't want to
buffer the whole thing and I don't want to spawn a new thread if I
don't have to.

I came up with a third idea and wrote a rough implementation.
Based on the command pattern I wrote a ControlledPostMethod (mabye
not be best name :) which extends EntityEnclosingMethod and
overwrote writeRequestBody(HttpState, HttpConnection).
ControlledPostMethod has a setRequestCommand(RequestCommand) method.
The passed in RequestCommand has an executeRequest(OutputStream) method that
gets executed in ControlledPostMethod#writeRequestBody().
That way I could write to the requests output stream in my custom
request command and had real streaming.  Nothing need to be buffered
and no thread was spawned.
The implementation is not cleaned up otherwise I would have posted
it here.

Though after I was done I realized that I don't realy like my own
implementation.  Obviously as I am new to HTTPClient and on this
mailing list I don't know the history for the current API design.
But it appears to me as if the "all-in-one" execute() method that
will send the request out on its way as well as retrieve the result,
leaves one with less control over what is going on during the
request.  I know this is a mouthful to say and I hope no one will be
offended by me asking this.  Any thoughts?

Regards,
Sebastian

Re: How to get POST request OutputStream?

Posted by Michael Becke <be...@u.washington.edu>.
As Oleg has said HttpClient uses an input stream for getting content 
instead of a providing an output stream.  To emulate HttpURLConnection 
I can think of a few options.

1) Buffer content to a file or byte array using something like:

ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(someObject);
oos.flush();
PostMethod post = new PostMethod();
post.setRequestBody(new ByteArrayInputStream(os.toByteArray()));

2) Use a PipedOutputStream.  Take a look at 
http://java.sun.com/j2se/1.3/docs/api/java/io/PipedOutputStream.html.  
Be sure to note that the PipedInputStream/OutputStream combo will have 
to be used from different threads.

Mike

On Saturday, May 17, 2003, at 12:45 PM, Sebastian Hauer wrote:

>
> Hi,
>
> I am kind of stuck here.  From the API and browsing through the code I
> don't seem to find a convenient way of obtaining an OutputStream.  I
> need this in order to rewrite some code that is currently using
> Sun's HttpURLConnection that is doing something like this:
>
> ----------------------------------------------
> HttpURLConnection conn = (HttpURLConnection) url.openConnection();
> conn.setRequestMethod("POST");
> OutputStream os = conn.getOutputStream();
> ObjectOutputStream oos = new ObjectOutputStream(os);
> oos.writeObject(someObject);
> oos.flush();
> ----------------------------------------------
>
> I will continue to look through the code but I assume I am missing
> something.  Any pointer in the right direction will be appreciated.
>
> Regards,
> Sebastian
>
> <mime-attachment>