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 Na...@emc.com on 2008/03/18 02:22:19 UTC

Send/Receive serialized object using Apache's httpclient package

Hi,

Is it possible send/receive serialized object using HttpClient? I always
get java.io.StreamCorruptedException: invalid stream header  Exception.

Here is my server side (servlet code)

    public void doPost (
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("application/x-www-form-urlencoded");
        ObjectOutputStream oos = new
ObjectOutputStream(response.getOutputStream());
        ArrayList l = new ArrayList();
        l.add("Index Server is running");
        oos.writeObject(l);
    }

Client side code that access this servlet

    public OutputStream sendRequest (String method, HashMap args)
        throws IOException, IndexServerCommunicationException
    {
        PostMethod post = new PostMethod(m_url);
        try
        {
		post.setRequestEntity(new
StringRequestEntity("something"));
            m_client.executeMethod(post);
            ObjectInputStream ois = new
ObjectInputStream(post.getResponseBodyAsStream());
		return ois;
        }
        finally
        {
            post.releaseConnection();
        }
    }

Any help is appreciated.

Thanks
-Valli


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


Re: Handling proxies and modifying request header fields

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2008-03-20 at 10:31 +0530, Biju P wrote:
> Hi,
> 
> I am writing an Applet. The applet must be accessed by users from 
> different domains and networks which are controlled by proxies and 
> firewalls.
> 
> My concerns are,
> i) The applet should be able to send requests to external websites thru 
> the proxy (if any proxy exists).
> ii) The applet should be able to modify the request header fields, such as 
> User-agent field.
> 
> When I try using normal URLConnection from java.io package, the applet is 
> able to inherit the proxy settings from the browser and send the requests 
> thru that proxy. But, I am getting response code 403 for some of the 
> sites. 
> 
> Can anybody tell me, is this possible by using Apache HttpClient?.

No, it is not. You have configure HttpClient manually either using
system properties or ProxySelector class if running on JRE 5.0 or newer.

Oleg

>  And if 
> so, please specify the version.
> 
> It would be a great help, if somebody could advise on this.
> 
> Biju P M
> Mailto: biju.p@tcs.com
> =====-----=====-----=====
> Notice: The information contained in this e-mail
> message and/or attachments to it may contain 
> confidential or privileged information. If you are 
> not the intended recipient, any dissemination, use, 
> review, distribution, printing or copying of the 
> information contained in this e-mail message 
> and/or attachments to it are strictly prohibited. If 
> you have received this communication in error, 
> please notify us by reply e-mail or telephone and 
> immediately and permanently delete the message 
> and any attachments. Thank you
> 
> 


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


Handling proxies and modifying request header fields

Posted by Biju P <bi...@tcs.com>.
Hi,

I am writing an Applet. The applet must be accessed by users from 
different domains and networks which are controlled by proxies and 
firewalls.

My concerns are,
i) The applet should be able to send requests to external websites thru 
the proxy (if any proxy exists).
ii) The applet should be able to modify the request header fields, such as 
User-agent field.

When I try using normal URLConnection from java.io package, the applet is 
able to inherit the proxy settings from the browser and send the requests 
thru that proxy. But, I am getting response code 403 for some of the 
sites. 

Can anybody tell me, is this possible by using Apache HttpClient?. And if 
so, please specify the version.

It would be a great help, if somebody could advise on this.

Biju P M
Mailto: biju.p@tcs.com
=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you



Re: Send/Receive serialized object using Apache's httpclient package

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2008-03-17 at 21:22 -0400, Natarajan_Valli@emc.com wrote:
> Hi,
> 
> Is it possible send/receive serialized object using HttpClient? 

Yes, it is


> I always
> get java.io.StreamCorruptedException: invalid stream header  Exception.
> 
> Here is my server side (servlet code)
> 
>     public void doPost (
>         HttpServletRequest request,
>         HttpServletResponse response)
>         throws ServletException, IOException
>     {
>         response.setContentType("application/x-www-form-urlencoded");

Content type is wrong. This has nothing to do with HTML forms

>         ObjectOutputStream oos = new
> ObjectOutputStream(response.getOutputStream());
>         ArrayList l = new ArrayList();
>         l.add("Index Server is running");
>         oos.writeObject(l);
>     }
> 
> Client side code that access this servlet
> 
>     public OutputStream sendRequest (String method, HashMap args)
>         throws IOException, IndexServerCommunicationException
>     {
>         PostMethod post = new PostMethod(m_url);
>         try
>         {
> 		post.setRequestEntity(new
> StringRequestEntity("something"));
>             m_client.executeMethod(post);
>             ObjectInputStream ois = new
> ObjectInputStream(post.getResponseBodyAsStream());
> 		return ois;

You _must_ not release the connection until you are done reading the
response body. No wonder you are getting StreamCorruptedException

Oleg


>         }
>         finally
>         {
>             post.releaseConnection();
>         }
>     }
> 
> Any help is appreciated.
> 
> Thanks
> -Valli
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 


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