hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marki <puto.c...@gmail.com>
Subject Re: Sending serialized object and stream
Date Thu, 22 May 2008 05:27:42 GMT
If it is useful from you, i succeded sending files through multiparts forms
in this way:

                    File aux = new File ("c:\\anyFILE");
                    HttpClient httpclient = new HttpClient();
                    PostMethod filePost = new PostMethod(urlCompleta);
                    filePost.setDoAuthentication(true);
                    Part[] parts = {new FilePart(aux.getName(), aux)};
                    filePost.setRequestEntity(new
MultipartRequestEntity(parts, filePost.getParams()));

httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
                    int status = httpclient.executeMethod(filePost);
                      if (status == HttpStatus.SC_OK) {
                          logger.info("sending OK...");

                    } else {
                         logger.info("sending ERROR...");
                     }

On Wed, May 21, 2008 at 10:45 PM, <Natarajan_Valli@emc.com> wrote:

> When MultiPartRequestEntity is used to send serialized object, the
> server (servlet) throws error "unexpected jdwp error: 35" while reading
> the object. I've attached code snippet from client and server
>
> Client code:
>
>            ByteArrayOutputStream requestStream = new
> ByteArrayOutputStream();
>            ObjectOutputStream objectOutput = new
> ObjectOutputStream(requestStream);
>            objectOutput.writeObject(request);
>            objectOutput.close();
>            String enc = "ISO-8859-1";
>
>            MultipartRequestEntity entity = new MultipartRequestEntity(
>                new Part[] {
>                    new FilePart(
>                        "param1",
>                        new ByteArrayPartSource("filename.txt",
> requestStream.toByteArray()),
>                         "application/octet-stream",
>                         enc) },
>                 post.getParams());
>            post.setRequestEntity(entity);
>
>            NameValuePair[] pair = new NameValuePair[1];
>            pair[0] = new NameValuePair(IRequest.HTTP_REQUEST_HANDLER,
> handler);
>            post.setQueryString(pair);
>            m_client.executeMethod(post);
>
> Server code:
>
>    public void doFilter(ServletRequest request, ServletResponse
> response, FilterChain chain)
>            throws IOException, ServletException {
>        if (m_abortErrorMessage != null) {
>            ((HttpServletResponse) response).sendError(500,
> m_abortErrorMessage);
>            return;
>        }
>
>        if (request instanceof HttpServletRequest) {
>            HttpServletRequest req = (HttpServletRequest) request;
>            String reqHandlerName =
> req.getParameter(IRequest.HTTP_REQUEST_HANDLER);
>            if (reqHandlerName != null) {
>                    InputStream reqInputStream = req.getInputStream();
>
>                    ObjectInputStream reqStream = new
> ObjectInputStream(reqInputStream); //this line throws the error
>                    IRequest reqObject = null;
>                    try {
>                        reqObject = (IRequest) reqStream.readObject();
>                    }
>                    catch (ClassNotFoundException e) {
>                        ((HttpServletResponse) response).sendError(400);
>
>                    }
>                }
>        }
>
>
> The same server code works fine when InputStreamRequestEntity is used,
> here is client code
>
>            ByteArrayOutputStream requestStream = new
> ByteArrayOutputStream();
>            ObjectOutputStream objectOutput = new
> ObjectOutputStream(requestStream);
>            objectOutput.writeObject(request);
>            objectOutput.close();
>
>            InputStream requestContent = new
> ByteArrayInputStream(requestStream.toByteArray());
>            if (requestContent != null) {
>                post.setRequestEntity(new
> InputStreamRequestEntity(requestContent));
>            }
>
>            NameValuePair[] pair = new NameValuePair[1];
>            pair[0] = new NameValuePair(IRequest.HTTP_REQUEST_HANDLER,
> handler);
>            post.setQueryString(pair);
>            m_client.executeMethod(post);
>
> We need to use multipart request entity.
>
> I appreciate help to solve this problem.
>
> Regards
> Valli
>
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:olegk@apache.org]
> Sent: Sunday, May 18, 2008 8:09 AM
> To: HttpClient User Discussion
> Subject: Re: Sending serialized object and stream
>
> On Fri, 2008-05-16 at 16:05 -0400, Natarajan_Valli@emc.com wrote:
> > Hi All,
> >
> > I've requirement to send both serialized java object and stream
> object.
> > Here is the code that sends serialized object
> >
> >             ByteArrayOutputStream requestStream = new
> > ByteArrayOutputStream();
> >             ObjectOutputStream objectOutput = new
> > ObjectOutputStream(requestStream);
> >             objectOutput.writeObject(request);
> >             objectOutput.close();
> >
> >             InputStream requestContent = new
> > ByteArrayInputStream(requestStream.toByteArray());
> >             if (requestContent != null) {
> >                 post.setRequestEntity(new
> > InputStreamRequestEntity(requestContent));
> >             }
> >
> > We need to optionally send stream object as well. I believe we need to
> > use multipart request entity. The HC currently supports FilePart and
> > StringPart. Do we need to write another type of Part like StreamPart
> to
> > do so?
> >
>
> Yes, you do
>
> Oleg
>
> > Help is appreciated.
> >
> > Thanks in advance
> >
> > 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
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>


-- 
Regards

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message