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 Mark Wolter <Ma...@gmx.de> on 2006/04/19 10:40:03 UTC

Buffered post example needed

Hello,

I'm searching for some example code with an buffered post to a server via
https.
What I was able to do is:
Reading from a file to a String and send the String as RequestBody:
...
File file = new File("c:\\temp\\ReportRequest.xml");
			
int x= fis.available();
byte b[]= new byte[x];
fis.read(b);
String content = new String(b);

post.setRequestBody(content);
...

But as this is a depracted method, I tried to read from the file via Stream:
...
File file = new File("c:\\temp\\ReportRequest.xml");

post.setRequestEntity(new InputStreamRequestEntity(new
FileInputStream(file)));

But here I'm getting the following exception:
org.apache.commons.httpclient.ProtocolException: Unbuffered entity enclosing
request can not be repeated.

When using "post.setRequestEntity(new InputStreamRequestEntity(new
FileInputStream(file), file.length()));" the other side (the server) can not
parse the xml correctly. 
The error code is:
org.xml.sax.SAXParseException: Premature end of file.


Any suggestions?




Thanks!



Mark


-- 
GMX Produkte empfehlen und ganz einfach Geld verdienen!
Satte Provisionen f�r GMX Partner: http://www.gmx.net/de/go/partner

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


Re: Buffered post example needed

Posted by Oleg Kalnichevski <ol...@apache.org>.
Mark Wolter wrote:
> Hi Oleg,
>
> thanks for the hint!
> I added the interface to my project, changed the code:
> post.setRequestEntity(new FileRequestEntity(file, "UTF-8"));
>
> and it worked!
>
> is there a possibility to download the whole package "
> org.apache.commons.httpclient.contrib.benchmark" ?
>
>
>
> Mark
>
>
>   
Hi Mark (apologies for calling you Mike in my previous mail)

The code in the 'contrib' package is provided as a reference material 
only and is not distributed in a binary form. Just make a copy of this 
class and tweak it any way you see fit.

Oleg
>   
>> --- Ursprüngliche Nachricht ---
>> Von: Oleg Kalnichevski <ol...@apache.org>
>> An: HttpClient User Discussion <ht...@jakarta.apache.org>
>> Betreff: Re: Buffered post example needed
>> Datum: Wed, 19 Apr 2006 21:17:57 +0200
>>
>> Mark Wolter wrote:
>>     
>>> Hello,
>>>
>>> I'm searching for some example code with an buffered post to a server
>>>       
>> via
>>     
>>> https.
>>> What I was able to do is:
>>> Reading from a file to a String and send the String as RequestBody:
>>> ...
>>> File file = new File("c:\\temp\\ReportRequest.xml");
>>> 			
>>> int x= fis.available();
>>> byte b[]= new byte[x];
>>> fis.read(b);
>>> String content = new String(b);
>>>
>>> post.setRequestBody(content);
>>> ...
>>>
>>> But as this is a depracted method, I tried to read from the file via
>>>       
>> Stream:
>>     
>>> ...
>>> File file = new File("c:\\temp\\ReportRequest.xml");
>>>
>>> post.setRequestEntity(new InputStreamRequestEntity(new
>>> FileInputStream(file)));
>>>
>>> But here I'm getting the following exception:
>>> org.apache.commons.httpclient.ProtocolException: Unbuffered entity
>>>       
>> enclosing
>>     
>>> request can not be repeated.
>>>
>>>   
>>>       
>> Mike,
>>
>> Consider implementing a custom request entity that encapsulates the 
>> request body and making it repeatable . Something similar to this one:
>>
>>     
> http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java
>   
>> Hope this helps
>>
>> Oleg
>>     
>>> When using "post.setRequestEntity(new InputStreamRequestEntity(new
>>> FileInputStream(file), file.length()));" the other side (the server) can
>>>       
>> not
>>     
>>> parse the xml correctly. 
>>> The error code is:
>>> org.xml.sax.SAXParseException: Premature end of file.
>>>
>>>
>>> Any suggestions?
>>>
>>>
>>>
>>>
>>> Thanks!
>>>
>>>
>>>
>>> Mark
>>>
>>>
>>>   
>>>       
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>>
>>     
>
>   


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


Re: Buffered post example needed

Posted by Mark Wolter <Ma...@gmx.de>.
Hi Oleg,

thanks for the hint!
I added the interface to my project, changed the code:
post.setRequestEntity(new FileRequestEntity(file, "UTF-8"));

and it worked!

is there a possibility to download the whole package "
org.apache.commons.httpclient.contrib.benchmark" ?



Mark



> --- Ursprüngliche Nachricht ---
> Von: Oleg Kalnichevski <ol...@apache.org>
> An: HttpClient User Discussion <ht...@jakarta.apache.org>
> Betreff: Re: Buffered post example needed
> Datum: Wed, 19 Apr 2006 21:17:57 +0200
> 
> Mark Wolter wrote:
> > Hello,
> >
> > I'm searching for some example code with an buffered post to a server
> via
> > https.
> > What I was able to do is:
> > Reading from a file to a String and send the String as RequestBody:
> > ...
> > File file = new File("c:\\temp\\ReportRequest.xml");
> > 			
> > int x= fis.available();
> > byte b[]= new byte[x];
> > fis.read(b);
> > String content = new String(b);
> >
> > post.setRequestBody(content);
> > ...
> >
> > But as this is a depracted method, I tried to read from the file via
> Stream:
> > ...
> > File file = new File("c:\\temp\\ReportRequest.xml");
> >
> > post.setRequestEntity(new InputStreamRequestEntity(new
> > FileInputStream(file)));
> >
> > But here I'm getting the following exception:
> > org.apache.commons.httpclient.ProtocolException: Unbuffered entity
> enclosing
> > request can not be repeated.
> >
> >   
> Mike,
> 
> Consider implementing a custom request entity that encapsulates the 
> request body and making it repeatable . Something similar to this one:
>
http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java
> 
> Hope this helps
> 
> Oleg
> > When using "post.setRequestEntity(new InputStreamRequestEntity(new
> > FileInputStream(file), file.length()));" the other side (the server) can
> not
> > parse the xml correctly. 
> > The error code is:
> > org.xml.sax.SAXParseException: Premature end of file.
> >
> >
> > Any suggestions?
> >
> >
> >
> >
> > Thanks!
> >
> >
> >
> > Mark
> >
> >
> >   
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 

-- 
Echte DSL-Flatrate dauerhaft für 0,- Euro*!
"Feel free" mit GMX DSL! http://www.gmx.net/de/go/dsl

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


Re: Buffered post example needed

Posted by Oleg Kalnichevski <ol...@apache.org>.
Mark Wolter wrote:
> Hello,
>
> I'm searching for some example code with an buffered post to a server via
> https.
> What I was able to do is:
> Reading from a file to a String and send the String as RequestBody:
> ...
> File file = new File("c:\\temp\\ReportRequest.xml");
> 			
> int x= fis.available();
> byte b[]= new byte[x];
> fis.read(b);
> String content = new String(b);
>
> post.setRequestBody(content);
> ...
>
> But as this is a depracted method, I tried to read from the file via Stream:
> ...
> File file = new File("c:\\temp\\ReportRequest.xml");
>
> post.setRequestEntity(new InputStreamRequestEntity(new
> FileInputStream(file)));
>
> But here I'm getting the following exception:
> org.apache.commons.httpclient.ProtocolException: Unbuffered entity enclosing
> request can not be repeated.
>
>   
Mike,

Consider implementing a custom request entity that encapsulates the 
request body and making it repeatable . Something similar to this one:
http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java

Hope this helps

Oleg
> When using "post.setRequestEntity(new InputStreamRequestEntity(new
> FileInputStream(file), file.length()));" the other side (the server) can not
> parse the xml correctly. 
> The error code is:
> org.xml.sax.SAXParseException: Premature end of file.
>
>
> Any suggestions?
>
>
>
>
> Thanks!
>
>
>
> Mark
>
>
>   


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