You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by santo <sa...@yahoo.com> on 2005/05/05 12:51:21 UTC

[HttpClient] send Vector object from servlet to client

Hi,

I'm writing an application where the client sends a request (GET) - using
HttpClient - to a servlet.

The request looks like this:

HttpClient client = new HttpClient();
client.setTimeout(timeout * 1000);
url = new URL(p_protocol, p_hostname, p_WebserverPort, file);
String strUrl = url.toExternalForm();
GetMethod method = new GetMethod(strUrl);

// Provide custom retry handler (seems to be necessary)
DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
retryhandler.setRequestSentRetryEnabled(false);
retryhandler.setRetryCount(1);
method.setMethodRetryHandler(retryhandler);

// Prevent CLOSE_WAITs
method.addRequestHeader( "Connection", "close");

// set content type
method.setRequestHeader("Content-type", "text/plain");

// add token
method.setRequestHeader("token", getHostname() + "-" + getID());


The servlet checks the token, and if that is valid it should send a 
Vector with strings back to the client.

Currently, the code looks like this:

String receivedToken = request.getHeader("token");
if (validToken()) {
  ObjectOutputStream oOut = new ObjectOutputStream(response.getOutputStream());
  oOut.write(Base64.encodeBase64(SerializationUtils.serialize(servletVector)));
  oOut.flush();
  oOut.close();
}


and this is the client code for reading the response:

Vector servletVector = (Vector)
  SerializationUtils.deserialize(
    Base64.decodeBase64(method.getResponseBodyAsString().getBytes()));


But when I run this code, I get following exception:
java.lang.ArrayIndexOutOfBoundsException: -84
  at org.apache.commons.codec.binary.Base64.isBase64(Base64.java:137)
  at org.apache.commons.codec.binary.Base64.discardNonBase64(Base64.java:478)
  at org.apache.commons.codec.binary.Base64.decodeBase64(Base64.java:374)
  at net.testpackage.Client.pullData(Client.java:1465)
  at net.testpackage.Client.run(Client.java:1971)


When leaving out the Base64 Encoding/Decoding, I get following exception:
org.apache.commons.lang.SerializationException: java.io.UTFDataFormatException
  at org.apache.commons.lang.
SerializationUtils.deserialize(SerializationUtils.java:204)
  at org.apache.commons.lang.
SerializationUtils.deserialize(SerializationUtils.java:229)
  at net.testpackage.Client.pullData(Client.java:1471)
  at net.testpackage.Client.run(Client.java:1978)
Caused by: java.io.UTFDataFormatException
  at java.io.ObjectInputStream$BlockDataInputStream.readUTFSpan(Unknown Source)
  at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(Unknown Source)
  at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
  at java.io.ObjectInputStream.readString(Unknown Source)
  at java.io.ObjectInputStream.readObject0(Unknown Source)
  at java.io.ObjectInputStream.readObject(Unknown Source)
  at org.apache.commons.lang.
SerializationUtils.deserialize(SerializationUtils.java:199)
  ... 3 more


I tried already several other methods (like converting the Vector to a
ByteArrayOutputStream) but until now I haven't succeeded to get it working.


Any help would be appreciated.


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