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 Albert Kam <mo...@gmail.com> on 2007/08/03 09:47:32 UTC

URL to HttpUtil problem in http tunelling

Dear developers,

I've been trying to convert the following codes using the standard
java.net.URL to the HttpClient solution. It's just a simple http tunelling
solution, sending and receiving java objects using http protocol. Here's the
source code using the java.net.URL :

    public static void main(String[] args) throws IOException,
ClassNotFoundException {
        URL url = new URL("
http://hostname:8080/util/servlet/servlet.GetObjectServlet");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        OutputStream outputStream = connection.getOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(new
BufferedOutputStream(outputStream));

          // creating an object to be sent to the servlet
        Map testMap = new HashMap();
        testMap.put("1", "one");
        testMap.put("2", "two");

        out.writeObject(testMap);
        out.flush();

          // read the object from the servlet
        InputStream inputStream = connection.getInputStream();
        ObjectInputStream in = new ObjectInputStream(new
BufferedInputStream(inputStream));
        Map map = (Map) in.readObject();
        System.out.println(map);
    }

I've been searching for solution, using the Apache HttpClient library,
because i need HttpClient's timeout feature. But so far, i'm stuck with how
to send a java object to another servlet. I have yet to find out how to get
the java object from the servlet using HttpUtil. I hope it's just as simple
as :

InputStream in = method.getResponseBodyAsStream();
BufferedInputStream bin = new BufferedInputStream(in);
ObjectInputStream objIn = new ObjectInputStream(bin);
GZIPInputStream gzipIn = new GZIPInputStream(objIn);
Object object = objIn.readObject();

Could please help me ? I dont know how to send a java object to a remote
servlet. I know it has something to do with the
PostMethod.setRequestEntity.. And i've seen the implementations of
RequestEntity like
InputStreamRequestEntity, FileRequestEntity, StringRequestEntity, but
there's no ObjectRequestEntity or something.

Any pointers ? Thank you.

Regards,
Albert Kam

Re: URL to HttpUtil problem in http tunelling

Posted by Albert Kam <mo...@gmail.com>.
Hello Roland,

Yes, i was quite embarrased about the fact that my friend paste me this url
AFTER i've sent my email to this mailing list ..
The solution is here :
http://mail-archives.apache.org/mod_mbox/commons-user/200408.mbox/%3C9A15B830AD54814FA79C20CA8924531906BA5712@lonex02.eu.turner.com%3E

And it works like magic, i've just tested.
So thank you everyone ! :)

On 8/3/07, Roland Weber <RO...@de.ibm.com> wrote:
>
> Hello Albert,
>
> > how to get
> > the java object from the servlet using HttpUtil. I hope it's just as
> simple
> > as :
> >
> > InputStream in = method.getResponseBodyAsStream();
> > BufferedInputStream bin = new BufferedInputStream(in);
> > ObjectInputStream objIn = new ObjectInputStream(bin);
> > GZIPInputStream gzipIn = new GZIPInputStream(objIn);
> > Object object = objIn.readObject();
>
> Leave out the gzip part, or decompress between buffered and
> object input stream. But basically, yes, that's how it works.
>
> > I dont know how to send a java object to a remote
> > servlet. I know it has something to do with the
> > PostMethod.setRequestEntity.. And i've seen the implementations of
> > RequestEntity like
> > InputStreamRequestEntity, FileRequestEntity, StringRequestEntity, but
> > there's no ObjectRequestEntity or something.
>
> RequestEntity is an interface. Implement the ObjectRequestEntity
> yourself, it's not that hard. I think this has also been discussed
> on one of the mailing lists before.
>
> hope that helps,
>   Roland
>
>
>


-- 
All meetings end in partings
That which rises must fall
That which is collected will be dispersed
Birth ends with death

Re: URL to HttpUtil problem in http tunelling

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Albert,

> how to get
> the java object from the servlet using HttpUtil. I hope it's just as 
simple
> as :
> 
> InputStream in = method.getResponseBodyAsStream();
> BufferedInputStream bin = new BufferedInputStream(in);
> ObjectInputStream objIn = new ObjectInputStream(bin);
> GZIPInputStream gzipIn = new GZIPInputStream(objIn);
> Object object = objIn.readObject();

Leave out the gzip part, or decompress between buffered and
object input stream. But basically, yes, that's how it works.

> I dont know how to send a java object to a remote
> servlet. I know it has something to do with the
> PostMethod.setRequestEntity.. And i've seen the implementations of
> RequestEntity like
> InputStreamRequestEntity, FileRequestEntity, StringRequestEntity, but
> there's no ObjectRequestEntity or something.

RequestEntity is an interface. Implement the ObjectRequestEntity
yourself, it's not that hard. I think this has also been discussed
on one of the mailing lists before.

hope that helps,
  Roland