hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sANTo <santo.pr...@gmail.com>
Subject Invalid stream header
Date Thu, 29 Jun 2006 15:53:30 GMT
I'm trying to send a Hashtable from my client application (using Jakarta
commons HttpClient) to my server (embedded Jetty 6.0),
but keep getting an "Invalid stream header" exception.

The code on the client looks like this:

private void sendToServer(Hashtable myTable) {
              // Serialize to a byte array
              ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
              ObjectOutputStream out = new ObjectOutputStream(bos) ;
              out.writeObject(myTable);
              out.close();

                System.out.println("array length: " + bos.toByteArray
().length);

              String encodedTable = factory.encode(bos.toByteArray());
              method.setRequestHeader("table", encodedTable);
              // withoud base64 encoding:
              //method.setRequestHeader("table", new String(bos.toByteArray
()));

              boolean responseOk = false;
              int statusCode = HttpStatus.SC_OK;
              byte[] responseBody;
              String response = null;
                  // First, make sure the response body is empty:
                  try {
                      responseBody = method.getResponseBody();
                      response = new String(responseBody);
                  }
                  catch (Exception exc) {
                      ; // just ignore, it only means there is no garbage in
the response body
                  }

                  // Execute the method.
                  statusCode = client.executeMethod(method);

                  // Read the response body.
                  ObjectInputStream oIn = new ObjectInputStream(
method.getResponseBodyAsStream());
                  response = (String) oIn.readObject();

                  // etc
}

on the server it goes like this:

private void handleRequest(HttpServletRequest request, HttpServletResponse
response) {
                    ObjectOutputStream oOut = new ObjectOutputStream(
response.getOutputStream());
                    response.setContentType("text/plain");

                    String encodedSerializedCompTable = request.getHeader
("table");
                    String decodedSerializedCompTable = factory.decode
(encodedSerializedCompTable);
                    byte[] decodedSerializedCompTableArray =
decodedSerializedCompTable.getBytes();
                    System.out.println("array length: " +
decodedSerializedCompTableArray.length);

                    ByteArrayInputStream bais = new
ByteArrayInputStream(decodedSerializedCompTableArray);
                    ObjectInputStream in = new ObjectInputStream(bais);   //
THIS CODE IS GIVING THE EXCEPTION
                    Hashtable components = (Hashtable) in.readObject();
                    in.close();

                   // etc
}


output on client:
array length: 337

output on server:
array length: 354

The exception I get is the following:
java.io.StreamCorruptedException: invalid stream header
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java
:753)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
    at org.mycomp.DAMServer$RequestHandler.handleRequest(DAMServer.java:741)
    at org.mycomp.DAMServer$RequestHandler.handleRequest(DAMServer.java:439)
    at org.mycomp.DAMServer$RequestHandler.handle(DAMServer.java:236)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java
:567)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(
ContextHandlerCollection.java:147)
    at org.mortbay.jetty.handler.HandlerCollection.handle(
HandlerCollection.java:126)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java
:119)
    at org.mortbay.jetty.Server.handle(Server.java:248)
    at org.mortbay.jetty.HttpConnection.handlerRequest(HttpConnection.java
:360)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(
HttpConnection.java:614)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:487)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:197)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:288)
    at org.mortbay.jetty.bio.SocketConnector$Connection.run(
SocketConnector.java:180)
    at org.mortbay.thread.BoundedThreadPool$PoolThread.run(
BoundedThreadPool.java:475)

I already tried with and without base64 encoding, but I can't get it
working.
Probably something obvious, but I don't see it

What am I doing wrong here ?

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