commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew van der Voort" <and...@astarte.co.nz>
Subject RE: [HttpClient] Can't even Post Data
Date Thu, 08 Jul 2004 04:32:58 GMT
Hmmmmm. Well wouldn't you know it that as soon as you start threatening
something, it starts behaving. I think the things I have done have had an
effect rather than anything else, but here it is...

I removed the Content-Type and Content-Length headers I was explicitly
setting into the PostMethod. Doing that pretty much got my
ByteArrayOutputSteam technique working. I was looking at the code I had and
was saying 'this is exactly what I have in my StringObjectRequestEntity' so
I thought it HAS to work.

So I pursued the RequestEntity approach and a bit more reading got me
looking at the chunking thing again. I gather from what I've seen that
ByteArrayRequestEntity uses chunking. So I overrode getContentLength() to
return EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED so that it works itself
out. This is deprecated so I have ended up with what is below. It now works
and I think the only reason why it works is because of the absence of
specifically written Header objects for Content-Type and -Length and the
setting of chunking to true. Doing that forces either httpclient or
java.net to work out the length by itself.

PostMethod method = new PostMethod(completeURL);
method.setContentChunked( true );
method.setRequestEntity( new StringObjectRequestEntity( requestBody ) );
int statusCode = httpClient.executeMethod( method );
... etc

public class StringObjectRequestEntity extends StringRequestEntity
{
   public StringObjectRequestEntity(String content)
    {
        super(content, "java-internal/" + String.class.getName(), null );
    }
    public StringObjectRequestEntity(String content, String contentType,
String charset)
    {
        super(content, contentType, charset);
    }
    public void writeRequest(OutputStream out) throws IOException
    {
        ObjectOutputStream oos = new ObjectOutputStream( out );
        oos.reset();
        oos.writeObject( this.getContent() );
        oos.flush();
    }
}

And all is good. The string is available at the server, and the correct
object is streamed and avilable back on the client.

So thanks for all your help!

Andrew

>-----Original Message-----
>From: Michael Becke [mailto:becke@u.washington.edu]
>Sent: Thursday, 8 July 2004 1:19 p.m.
>To: Jakarta Commons Users List
>Subject: Re: [HttpClient] Can't even Post Data
>
>
>Hi Andrew,
>
>It sounds like you are doing everything correctly.  I also know that a 
>number of people use HttpClient for exactly what you are 
>describing, so 
>it is possible.  If you post a wire log 
><http://jakarta.apache.org/commons/httpclient/logging.html> of 
>the post 
>it should help to narrow things down.
>
>Mike
>
>On Jul 7, 2004, at 7:29 PM, Andrew van der Voort wrote:
>
>> OK, got diverted onto real work for awhile there...
>>
>> I tried setting the content length header (though java streaming 
>> results in
>> a stream longer than the length of the data, I think). I 
>also see that
>> StringRequestEntity supplies its length already so did 
>nothing there to
>> mess with that in my StringObjectRequestEntity.
>>
>> I have also tried the following instead of using a custom requet 
>> entity:
>> ByteArrayOutputStream os = new ByteArrayOutputStream();
>> ObjectOutputStream oos = new ObjectOutputStream( os );
>> oos.reset();
>> oos.writeObject( myString );
>> oos.flush();
>> method.setRequestEntity( new ByteArrayRequestEntity( 
>os.toByteArray(),
>> "java-internal/java.lang.String" ) );
>>
>> but I still get an EOFException at the server.
>>
>> I don't know where to go from here with HttpClient. Surely 
>it should be
>> easier than this...
>>
>> Andrew
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

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


Mime
View raw message