You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Zhaohua Meng <zh...@gmail.com> on 2008/09/17 06:20:30 UTC

how to use PostMethod to stream request?

I have code snippet like the following. After establishing a
connection, I need to steam content from database as post body. How do
I get a OutputStream from either HttpClient or PostMethod? With
java.net.URL I can use openConnection().getOutputStream(). However I
want to leverage the nice features of HttpCleint. In the following
code, I want to call the writeContent() method which should get huge
data from database.

Any help is appreciated.

	public static void main(String[] args)  throws Exception {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://myhost/service/consumer");
		try {
                       //how to get a OutputStream to write my content?

			int status = client.executeMethod(post);

			System.out.println(post.getResponseBodyAsString());
		} finally {
			post.releaseConnection();
		}		
	}
	public static void writeContent(OutputStream out) throws IOException {
                //data will be generated from database.
		out.write("<data><value>this</value></data>".getBytes());
	}
}

-- 
Zhaohua Meng
zhaohua.meng@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to use PostMethod to stream request?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2008-09-23 at 11:40 -0700, Yan aka. Yansheng wrote:
> Hi Oleg,
> 
> See attached message for my use case.  Sorry if I misunderstood OP's use
> case.  But the answer I got from you is that HTTPClient 3.x API was not
> meant to be used that way...
> 

One is not meant to have a direct access to the underlying
HttpConnection object. The reason for it being, if the HttpConnection is
used directly, HttpClient may no longer know if that connection is
valid / safe to reuse.  

That does not mean one cannot stream out content to the underlying
output stream.

Hope that clarifies things a little

Oleg  


> As always, appreciate your help!
> 
> 
> 
> On Fri, 2008-06-27 at 10:43 -0700, Yan aka. Yansheng wrote:
> > Hi There,
> >
> > This is my first post here so I hope this is the right audience.
> >
> > My story: I want to replace a chunk of my code for doing I/O streaming to
> a
> > webservice.  I use java.net.HttpURLConnection to get the InputStream and
> > OutputStream, it works fine most of the time but it can get a bit flaky
> and
> > I had to write my own retry logic.  So I decided to switch to
> > jakartacommons-httpclient to see if things get better.  The problem is I
> > don't seem to be able to come up with a good replacement from the
> httpclient
> > package.
> >
> > Here is the method for obtaining a HttpConnection I wrote, the problem is
> > the HostConfiguration returned is not populated.
> >
> 
> Yansheng
> 
> HttpClient 3.x API was never meant to be used this way. You should
> either let HttpClient manage connections, or use connection management
> components from HttpClient 4.0
> 
> Hope this helps
> 
> Oleg
> 
> 
> 
> On Thu, Sep 18, 2008 at 10:56 AM, Oleg Kalnichevski <ol...@apache.org>wrote:
> 
> > On Thu, 2008-09-18 at 08:16 -0700, Yan aka. Yansheng wrote:
> > > The example you provided use the File as the buffer and then send as it
> > over
> > > the network a RequestEntity.  There is not direct IO writes to an http
> > > connection in HttpClient.
> >
> > What you are saying is plain WRONG. RequestEntity#writeRequest() method
> > gives you a direct access to the output stream of the underlying
> > connection, which can be used to stream out any arbitrary content. The
> > entity implementation given as an example does not use a file as a
> > buffer. It streams out content of a file by reading from the file input
> > stream and writing directly to the output stream of the HTTP
> > connection.
> >
> > Please do not make statements about things you are not sure about.
> >
> > Oleg
> >
> >
> > > On Wed, Sep 17, 2008 at 10:52 AM, Oleg Kalnichevski <olegk@apache.org
> > >wrote:
> > >
> > > > On Wed, 2008-09-17 at 08:27 -0700, Yan aka. Yansheng wrote:
> > > > > I had a similar problem.  From what I gathered, HttpClient does not
> > > > support
> > > > > stream IO directly.  It's not meant for streaming IO.
> > > >
> > > > This is most certainly is _NOT_ the case. HttpClient is fully capable
> > of
> > > > streaming data in and out since version 2.0.
> > > >
> > > > Oleg
> > > >
> > > > >   So one solution is to
> > > > > buffer your output locally and send in batch at the end.
> > > > >
> > > > > Hope this helps.
> > > > >
> > > > > Yansheng Lin
> > > > >
> > > >
> > > >
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > > >
> > > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to use PostMethod to stream request?

Posted by "Yan aka. Yansheng" <ya...@gmail.com>.
Hi Oleg,

See attached message for my use case.  Sorry if I misunderstood OP's use
case.  But the answer I got from you is that HTTPClient 3.x API was not
meant to be used that way...

As always, appreciate your help!



On Fri, 2008-06-27 at 10:43 -0700, Yan aka. Yansheng wrote:
> Hi There,
>
> This is my first post here so I hope this is the right audience.
>
> My story: I want to replace a chunk of my code for doing I/O streaming to
a
> webservice.  I use java.net.HttpURLConnection to get the InputStream and
> OutputStream, it works fine most of the time but it can get a bit flaky
and
> I had to write my own retry logic.  So I decided to switch to
> jakartacommons-httpclient to see if things get better.  The problem is I
> don't seem to be able to come up with a good replacement from the
httpclient
> package.
>
> Here is the method for obtaining a HttpConnection I wrote, the problem is
> the HostConfiguration returned is not populated.
>

Yansheng

HttpClient 3.x API was never meant to be used this way. You should
either let HttpClient manage connections, or use connection management
components from HttpClient 4.0

Hope this helps

Oleg



On Thu, Sep 18, 2008 at 10:56 AM, Oleg Kalnichevski <ol...@apache.org>wrote:

> On Thu, 2008-09-18 at 08:16 -0700, Yan aka. Yansheng wrote:
> > The example you provided use the File as the buffer and then send as it
> over
> > the network a RequestEntity.  There is not direct IO writes to an http
> > connection in HttpClient.
>
> What you are saying is plain WRONG. RequestEntity#writeRequest() method
> gives you a direct access to the output stream of the underlying
> connection, which can be used to stream out any arbitrary content. The
> entity implementation given as an example does not use a file as a
> buffer. It streams out content of a file by reading from the file input
> stream and writing directly to the output stream of the HTTP
> connection.
>
> Please do not make statements about things you are not sure about.
>
> Oleg
>
>
> > On Wed, Sep 17, 2008 at 10:52 AM, Oleg Kalnichevski <olegk@apache.org
> >wrote:
> >
> > > On Wed, 2008-09-17 at 08:27 -0700, Yan aka. Yansheng wrote:
> > > > I had a similar problem.  From what I gathered, HttpClient does not
> > > support
> > > > stream IO directly.  It's not meant for streaming IO.
> > >
> > > This is most certainly is _NOT_ the case. HttpClient is fully capable
> of
> > > streaming data in and out since version 2.0.
> > >
> > > Oleg
> > >
> > > >   So one solution is to
> > > > buffer your output locally and send in batch at the end.
> > > >
> > > > Hope this helps.
> > > >
> > > > Yansheng Lin
> > > >
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > >
> > >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: how to use PostMethod to stream request?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2008-09-18 at 08:16 -0700, Yan aka. Yansheng wrote:
> The example you provided use the File as the buffer and then send as it over
> the network a RequestEntity.  There is not direct IO writes to an http
> connection in HttpClient.

What you are saying is plain WRONG. RequestEntity#writeRequest() method
gives you a direct access to the output stream of the underlying
connection, which can be used to stream out any arbitrary content. The
entity implementation given as an example does not use a file as a
buffer. It streams out content of a file by reading from the file input
stream and writing directly to the output stream of the HTTP
connection.  

Please do not make statements about things you are not sure about.  

Oleg


> On Wed, Sep 17, 2008 at 10:52 AM, Oleg Kalnichevski <ol...@apache.org>wrote:
> 
> > On Wed, 2008-09-17 at 08:27 -0700, Yan aka. Yansheng wrote:
> > > I had a similar problem.  From what I gathered, HttpClient does not
> > support
> > > stream IO directly.  It's not meant for streaming IO.
> >
> > This is most certainly is _NOT_ the case. HttpClient is fully capable of
> > streaming data in and out since version 2.0.
> >
> > Oleg
> >
> > >   So one solution is to
> > > buffer your output locally and send in batch at the end.
> > >
> > > Hope this helps.
> > >
> > > Yansheng Lin
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to use PostMethod to stream request?

Posted by Zhaohua Meng <zh...@gmail.com>.
I was able to use java.net classes to make my code work. Apparently,
Sun's URLConnection has internal buffer to handle the request
streaming without content length pre-calculated. It sets the
Content-Length header correctly in my test with small data. Later I
will find out if it can deal with large data. Ideally it should switch
to chunked transfer encoding automatically.

Thanks,

On Thu, Sep 18, 2008 at 11:16 AM, Yan aka. Yansheng
<ya...@gmail.com> wrote:
> The example you provided use the File as the buffer and then send as it over
> the network a RequestEntity.  There is not direct IO writes to an http
> connection in HttpClient.
>
> On Wed, Sep 17, 2008 at 10:52 AM, Oleg Kalnichevski <ol...@apache.org>wrote:
>
>> On Wed, 2008-09-17 at 08:27 -0700, Yan aka. Yansheng wrote:
>> > I had a similar problem.  From what I gathered, HttpClient does not
>> support
>> > stream IO directly.  It's not meant for streaming IO.
>>
>> This is most certainly is _NOT_ the case. HttpClient is fully capable of
>> streaming data in and out since version 2.0.
>>
>> Oleg
>>
>> >   So one solution is to
>> > buffer your output locally and send in batch at the end.
>> >
>> > Hope this helps.
>> >
>> > Yansheng Lin
>> >
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>>
>



-- 
Zhaohua Meng
zhaohua.meng@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to use PostMethod to stream request?

Posted by "Yan aka. Yansheng" <ya...@gmail.com>.
The example you provided use the File as the buffer and then send as it over
the network a RequestEntity.  There is not direct IO writes to an http
connection in HttpClient.

On Wed, Sep 17, 2008 at 10:52 AM, Oleg Kalnichevski <ol...@apache.org>wrote:

> On Wed, 2008-09-17 at 08:27 -0700, Yan aka. Yansheng wrote:
> > I had a similar problem.  From what I gathered, HttpClient does not
> support
> > stream IO directly.  It's not meant for streaming IO.
>
> This is most certainly is _NOT_ the case. HttpClient is fully capable of
> streaming data in and out since version 2.0.
>
> Oleg
>
> >   So one solution is to
> > buffer your output locally and send in batch at the end.
> >
> > Hope this helps.
> >
> > Yansheng Lin
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: how to use PostMethod to stream request?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2008-09-17 at 08:27 -0700, Yan aka. Yansheng wrote:
> I had a similar problem.  From what I gathered, HttpClient does not support
> stream IO directly.  It's not meant for streaming IO.

This is most certainly is _NOT_ the case. HttpClient is fully capable of
streaming data in and out since version 2.0. 

Oleg

>   So one solution is to
> buffer your output locally and send in batch at the end.
> 
> Hope this helps.
> 
> Yansheng Lin
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to use PostMethod to stream request?

Posted by "Yan aka. Yansheng" <ya...@gmail.com>.
I had a similar problem.  From what I gathered, HttpClient does not support
stream IO directly.  It's not meant for streaming IO.  So one solution is to
buffer your output locally and send in batch at the end.

Hope this helps.

Yansheng Lin

On Tue, Sep 16, 2008 at 9:20 PM, Zhaohua Meng <zh...@gmail.com>wrote:

> I have code snippet like the following. After establishing a
> connection, I need to steam content from database as post body. How do
> I get a OutputStream from either HttpClient or PostMethod? With
> java.net.URL I can use openConnection().getOutputStream(). However I
> want to leverage the nice features of HttpCleint. In the following
> code, I want to call the writeContent() method which should get huge
> data from database.
>
> Any help is appreciated.
>
>        public static void main(String[] args)  throws Exception {
>                HttpClient client = new HttpClient();
>                PostMethod post = new PostMethod("
> http://myhost/service/consumer");
>                try {
>                       //how to get a OutputStream to write my content?
>
>                        int status = client.executeMethod(post);
>
>                        System.out.println(post.getResponseBodyAsString());
>                } finally {
>                        post.releaseConnection();
>                }
>        }
>        public static void writeContent(OutputStream out) throws IOException
> {
>                //data will be generated from database.
>                out.write("<data><value>this</value></data>".getBytes());
>        }
> }
>
> --
> Zhaohua Meng
> zhaohua.meng@gmail.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: how to use PostMethod to stream request?

Posted by Zhaohua Meng <zh...@gmail.com>.
Oleg,

I guess in my case it is not repeatable since in
writeRequest(OutputStream), I need to get the business data and send
them to the OutputStream uninterrupted.

Thank you very much for the help.

On Wed, Sep 17, 2008 at 5:41 PM, Oleg Kalnichevski <ol...@apache.org> wrote:
> On Wed, 2008-09-17 at 17:35 -0400, Zhaohua Meng wrote:
>> Oleg,
>>
>> Thanks for the pointer. I implemented the RequestEntity and the
>> streaming is working fine if I know the content length. However the
>> content is generated dynamically from business data. As is specified
>> in the RequestEntity, my getContentLength() returns -1. However I'm
>> getting the following exception. Do I need to implement anything else?
>>
>> Thanks,
>>
>
> If the content is repeatable, that is, it can be reliably generated by
> the entity more than once, the entity should indicate so by returning
> true from RequestEntity#isRepeatable().
>
> If the content is not repeatable, then the request enclosing such entity
> cannot be retried for obvious reasons.
>
> Hope this helps
>
> Oleg
>
>
>
>>
>> Sep 17, 2008 5:23:47 PM
>> org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
>> INFO: I/O exception
>> (org.apache.commons.httpclient.NoHttpResponseException) caught when
>> processing request: The server www.apache.org failed to respond
>> Sep 17, 2008 5:23:47 PM
>> org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
>> INFO: Retrying request
>> org.apache.commons.httpclient.ProtocolException: Unbuffered entity
>> enclosing request can not be repeated.
>>       at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:487)
>>       at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
>>       at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
>>       at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
>>       at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
>>       at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
>>       at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
>>       at com.cgsh.poc.httpclient.ProxyAuthenticationTest.doHttpClient(ProxyAuthenticationTest.java:36)
>>       at com.cgsh.poc.httpclient.ProxyAuthenticationTest.main(ProxyAuthenticationTest.java:26)
>>
>>
>> On Wed, Sep 17, 2008 at 1:56 PM, Oleg Kalnichevski <ol...@apache.org> wrote:
>> > On Wed, 2008-09-17 at 00:20 -0400, Zhaohua Meng wrote:
>> >> I have code snippet like the following. After establishing a
>> >> connection, I need to steam content from database as post body. How do
>> >> I get a OutputStream from either HttpClient or PostMethod? With
>> >> java.net.URL I can use openConnection().getOutputStream(). However I
>> >> want to leverage the nice features of HttpCleint. In the following
>> >> code, I want to call the writeContent() method which should get huge
>> >> data from database.
>> >>
>> >
>> > Zhaohua,
>> >
>> > You need to implement a custom RequestEntity
>> >
>> > http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/RequestEntity.html
>> >
>> > You can use the FileRequestEntity class shipped with HttpClient 3.1 as
>> > an example
>> >
>> > http://hc.apache.org/httpclient-3.x/xref/org/apache/commons/httpclient/methods/FileRequestEntity.html
>> >
>> > Hope this helps
>> >
>> > Oleg
>> >
>> >
>> >> Any help is appreciated.
>> >>
>> >>       public static void main(String[] args)  throws Exception {
>> >>               HttpClient client = new HttpClient();
>> >>               PostMethod post = new PostMethod("http://myhost/service/consumer");
>> >>               try {
>> >>                        //how to get a OutputStream to write my content?
>> >>
>> >>                       int status = client.executeMethod(post);
>> >>
>> >>                       System.out.println(post.getResponseBodyAsString());
>> >>               } finally {
>> >>                       post.releaseConnection();
>> >>               }
>> >>       }
>> >>       public static void writeContent(OutputStream out) throws IOException {
>> >>                 //data will be generated from database.
>> >>               out.write("<data><value>this</value></data>".getBytes());
>> >>       }
>> >> }
>> >>
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
>> >
>> >
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>



-- 
Zhaohua Meng
zhaohua.meng@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to use PostMethod to stream request?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2008-09-17 at 17:35 -0400, Zhaohua Meng wrote:
> Oleg,
> 
> Thanks for the pointer. I implemented the RequestEntity and the
> streaming is working fine if I know the content length. However the
> content is generated dynamically from business data. As is specified
> in the RequestEntity, my getContentLength() returns -1. However I'm
> getting the following exception. Do I need to implement anything else?
> 
> Thanks,
> 

If the content is repeatable, that is, it can be reliably generated by
the entity more than once, the entity should indicate so by returning
true from RequestEntity#isRepeatable().

If the content is not repeatable, then the request enclosing such entity
cannot be retried for obvious reasons.

Hope this helps

Oleg



> 
> Sep 17, 2008 5:23:47 PM
> org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
> INFO: I/O exception
> (org.apache.commons.httpclient.NoHttpResponseException) caught when
> processing request: The server www.apache.org failed to respond
> Sep 17, 2008 5:23:47 PM
> org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
> INFO: Retrying request
> org.apache.commons.httpclient.ProtocolException: Unbuffered entity
> enclosing request can not be repeated.
> 	at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:487)
> 	at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
> 	at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
> 	at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
> 	at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
> 	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
> 	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
> 	at com.cgsh.poc.httpclient.ProxyAuthenticationTest.doHttpClient(ProxyAuthenticationTest.java:36)
> 	at com.cgsh.poc.httpclient.ProxyAuthenticationTest.main(ProxyAuthenticationTest.java:26)
> 
> 
> On Wed, Sep 17, 2008 at 1:56 PM, Oleg Kalnichevski <ol...@apache.org> wrote:
> > On Wed, 2008-09-17 at 00:20 -0400, Zhaohua Meng wrote:
> >> I have code snippet like the following. After establishing a
> >> connection, I need to steam content from database as post body. How do
> >> I get a OutputStream from either HttpClient or PostMethod? With
> >> java.net.URL I can use openConnection().getOutputStream(). However I
> >> want to leverage the nice features of HttpCleint. In the following
> >> code, I want to call the writeContent() method which should get huge
> >> data from database.
> >>
> >
> > Zhaohua,
> >
> > You need to implement a custom RequestEntity
> >
> > http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/RequestEntity.html
> >
> > You can use the FileRequestEntity class shipped with HttpClient 3.1 as
> > an example
> >
> > http://hc.apache.org/httpclient-3.x/xref/org/apache/commons/httpclient/methods/FileRequestEntity.html
> >
> > Hope this helps
> >
> > Oleg
> >
> >
> >> Any help is appreciated.
> >>
> >>       public static void main(String[] args)  throws Exception {
> >>               HttpClient client = new HttpClient();
> >>               PostMethod post = new PostMethod("http://myhost/service/consumer");
> >>               try {
> >>                        //how to get a OutputStream to write my content?
> >>
> >>                       int status = client.executeMethod(post);
> >>
> >>                       System.out.println(post.getResponseBodyAsString());
> >>               } finally {
> >>                       post.releaseConnection();
> >>               }
> >>       }
> >>       public static void writeContent(OutputStream out) throws IOException {
> >>                 //data will be generated from database.
> >>               out.write("<data><value>this</value></data>".getBytes());
> >>       }
> >> }
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to use PostMethod to stream request?

Posted by Zhaohua Meng <zh...@gmail.com>.
Oleg,

Thanks for the pointer. I implemented the RequestEntity and the
streaming is working fine if I know the content length. However the
content is generated dynamically from business data. As is specified
in the RequestEntity, my getContentLength() returns -1. However I'm
getting the following exception. Do I need to implement anything else?

Thanks,


Sep 17, 2008 5:23:47 PM
org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception
(org.apache.commons.httpclient.NoHttpResponseException) caught when
processing request: The server www.apache.org failed to respond
Sep 17, 2008 5:23:47 PM
org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
org.apache.commons.httpclient.ProtocolException: Unbuffered entity
enclosing request can not be repeated.
	at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:487)
	at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
	at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
	at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
	at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
	at com.cgsh.poc.httpclient.ProxyAuthenticationTest.doHttpClient(ProxyAuthenticationTest.java:36)
	at com.cgsh.poc.httpclient.ProxyAuthenticationTest.main(ProxyAuthenticationTest.java:26)


On Wed, Sep 17, 2008 at 1:56 PM, Oleg Kalnichevski <ol...@apache.org> wrote:
> On Wed, 2008-09-17 at 00:20 -0400, Zhaohua Meng wrote:
>> I have code snippet like the following. After establishing a
>> connection, I need to steam content from database as post body. How do
>> I get a OutputStream from either HttpClient or PostMethod? With
>> java.net.URL I can use openConnection().getOutputStream(). However I
>> want to leverage the nice features of HttpCleint. In the following
>> code, I want to call the writeContent() method which should get huge
>> data from database.
>>
>
> Zhaohua,
>
> You need to implement a custom RequestEntity
>
> http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/RequestEntity.html
>
> You can use the FileRequestEntity class shipped with HttpClient 3.1 as
> an example
>
> http://hc.apache.org/httpclient-3.x/xref/org/apache/commons/httpclient/methods/FileRequestEntity.html
>
> Hope this helps
>
> Oleg
>
>
>> Any help is appreciated.
>>
>>       public static void main(String[] args)  throws Exception {
>>               HttpClient client = new HttpClient();
>>               PostMethod post = new PostMethod("http://myhost/service/consumer");
>>               try {
>>                        //how to get a OutputStream to write my content?
>>
>>                       int status = client.executeMethod(post);
>>
>>                       System.out.println(post.getResponseBodyAsString());
>>               } finally {
>>                       post.releaseConnection();
>>               }
>>       }
>>       public static void writeContent(OutputStream out) throws IOException {
>>                 //data will be generated from database.
>>               out.write("<data><value>this</value></data>".getBytes());
>>       }
>> }
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>



-- 
Zhaohua Meng
zhaohua.meng@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to use PostMethod to stream request?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2008-09-17 at 00:20 -0400, Zhaohua Meng wrote:
> I have code snippet like the following. After establishing a
> connection, I need to steam content from database as post body. How do
> I get a OutputStream from either HttpClient or PostMethod? With
> java.net.URL I can use openConnection().getOutputStream(). However I
> want to leverage the nice features of HttpCleint. In the following
> code, I want to call the writeContent() method which should get huge
> data from database.
> 

Zhaohua,

You need to implement a custom RequestEntity

http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/RequestEntity.html

You can use the FileRequestEntity class shipped with HttpClient 3.1 as
an example

http://hc.apache.org/httpclient-3.x/xref/org/apache/commons/httpclient/methods/FileRequestEntity.html

Hope this helps

Oleg


> Any help is appreciated.
> 
> 	public static void main(String[] args)  throws Exception {
> 		HttpClient client = new HttpClient();
> 		PostMethod post = new PostMethod("http://myhost/service/consumer");
> 		try {
>                        //how to get a OutputStream to write my content?
> 
> 			int status = client.executeMethod(post);
> 
> 			System.out.println(post.getResponseBodyAsString());
> 		} finally {
> 			post.releaseConnection();
> 		}		
> 	}
> 	public static void writeContent(OutputStream out) throws IOException {
>                 //data will be generated from database.
> 		out.write("<data><value>this</value></data>".getBytes());
> 	}
> }
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org