hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Zhaohua Meng" <zhaohua.m...@gmail.com>
Subject Re: how to use PostMethod to stream request?
Date Wed, 17 Sep 2008 21:35:43 GMT
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 <olegk@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


Mime
View raw message