hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: ContentProducer.writeTo() not called?
Date Tue, 10 Mar 2009 20:23:29 GMT
Florent Georges wrote:
> Florent Georges wrote:
> 
>>   I am trying to use EntityTemplate with a custom, simple
>> ContentProducer.  But it seems the content is never written
>> to the wire.
> 
>   After having investigated further, I found the following weird
> situation.  If I write a proxy HttpEntity, that wraps the
> EntityTemplate, it seems writeTo() is never called because
> getContentLength() does return -1. 

I am not sure I understand. What proxy entity? What does this all have 
to do with getContentLength() returning -1?

The following code works just fine for me. See wire log below.

---
EntityTemplate t = new EntityTemplate(
         new ContentProducer() {

             public void writeTo(OutputStream outstream)
                 throws IOException {
                 outstream.write("<content/>".getBytes());
             }

         });
t.setContentType("application/xml");
HttpPost httppost = new 
HttpPost("http://localhost:8080/examples/servlets/servlet/RequestInfoExample");
httppost.setEntity(t);

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpContext localContext = new BasicHttpContext();

HttpResponse response = httpclient.execute(httppost, localContext);
HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
     System.out.println("Response content length: " +
         entity.getContentLength());
}
if (entity != null) {
     entity.consumeContent();
}
---

---
[DEBUG] wire - >> "POST /examples/servlets/servlet/RequestInfoExample 
HTTP/1.1[EOL]"
[DEBUG] wire - >> "Transfer-Encoding: chunked[EOL]"
[DEBUG] wire - >> "Content-Type: application/xml[EOL]"
[DEBUG] wire - >> "Host: localhost:8080[EOL]"
[DEBUG] wire - >> "Connection: Keep-Alive[EOL]"
[DEBUG] wire - >> "User-Agent: Apache-HttpClient/4.0-beta3-SNAPSHOT 
(java 1.5)[EOL]"
[DEBUG] wire - >> "Expect: 100-Continue[EOL]"
[DEBUG] wire - >> "[EOL]"
[DEBUG] wire - << "HTTP/1.1 100 Continue[EOL]"
[DEBUG] wire - >> "a[EOL]"
[DEBUG] wire - >> "<content/>"
[DEBUG] wire - >> "[EOL]"
[DEBUG] wire - >> "0[EOL]"
[DEBUG] wire - >> "[EOL]"
[DEBUG] wire - << "HTTP/1.1 200 OK[EOL]"
[DEBUG] wire - << "Server: Apache-Coyote/1.1[EOL]"
[DEBUG] wire - << "Content-Type: text/html[EOL]"
[DEBUG] wire - << "Content-Length: 690[EOL]"
[DEBUG] wire - << "Date: Tue, 10 Mar 2009 20:22:36 GMT[EOL]"
----------------------------------------
HTTP/1.1 200 OK
[DEBUG] wire - << "<html>[\r][\n]"
[DEBUG] wire - << "<body>[\r][\n]"
[DEBUG] wire - << "<head>[\r][\n]"
[DEBUG] wire - << "<title>Request Information Example</title>[\r][\n]"
[DEBUG] wire - << "</head>[\r][\n]"
[DEBUG] wire - << "<body bgcolor="white">[\r][\n]"
[DEBUG] wire - << "<a href="../reqinfo.html">[\r][\n]"
[DEBUG] wire - << "<img src="../images/code.gif" height=24 width=24 
align=right border=0 alt="view code"></a>[\r][\n]"
[DEBUG] wire - << "<a href="../index.html">[\r][\n]"Response content 
length: 690

[DEBUG] wire - << "<img src="../images/return.gif" height=24 width=24 
align=right border=0 alt="return"></a>[\r][\n]"
[DEBUG] wire - << "<h3>Request Information Example</h3>[\r][\n]"
[DEBUG] wire - << "<table border=0><tr><td>[\r][\n]"
[DEBUG] wire - << "Method:[\r][\n]"
[DEBUG] wire - << "</td><td>[\r][\n]"
[DEBUG] wire - << "POST[\r][\n]"
[DEBUG] wire - << "</td></tr><tr><td>[\r][\n]"
[DEBUG] wire - << "Request URI:[\r][\n]"
[DEBUG] wire - << "</td><td>[\r][\n]"
[DEBUG] wire - << "/examples/servlets/servlet/RequestInfoExample[\r][\n]"
[DEBUG] wire - << "</td></tr><tr><td>[\r][\n]"
[DEBUG] wire - << "Protocol:[\r][\n]"
[DEBUG] wire - << "</td><td>[\r][\n]"
[DEBUG] wire - << "HTTP/1.1[\r][\n]"
[DEBUG] wire - << "</td></tr><tr><td>[\r][\n]"
[DEBUG] wire - << "Path Info:[\r][\n]"
[DEBUG] wire - << "</td><td>[\r][\n]"
[DEBUG] wire - << "null[\r][\n]"
[DEBUG] wire - << "</td></tr><tr><td>[\r][\n]"
[DEBUG] wire - << "Remote Address:[\r][\n]"
[DEBUG] wire - << "</td><td>[\r][\n]"
[DEBUG] wire - << "127.0.0.1[\r][\n]"
[DEBUG] wire - << "</table>[\r][\n]"
---

Oleg


  If this proxy entity returns
> instead the length of the string, everything goes just fine.
> 
>   Of course, this is possible in this simple example, but in my
> real case I don't know the length of the content and I would
> just want to be able to write it directly to the output stream
> as it is produced.
> 
>   Is there a way to achieve that, without buffer the whole
> content?
> 
>   Kind regards,
> 


---------------------------------------------------------------------
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