hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sANTo <santo.pr...@gmail.com>
Subject garbage in responsebody
Date Mon, 26 Jun 2006 10:27:00 GMT
I'm writing a client/server app where the client uses httpclient to retrieve
data from the server (which uses Jetty or tomcat)
Whenever the client retrieves the responsebody using method.getResponseBody(),
the responsebody contains some garbage before the actual content.

For example, when the server returns the string "distribute" (see code
below), the actual content of method.getResponseBody() is:

response[0]:-84
response[1]:-19
response[2]:0
response[3]:5
response[4]:116
response[5]:0
response[6]:10
response[7]:100
response[8]:105
response[9]:115
response[10]:116
response[11]:114
response[12]:105
response[13]:98
response[14]:117
response[15]:116
response[16]:101

or "translated":

response[0]: ¬
response[1]: í
response[2]:NUL
response[3]:ENQ
response[4]:t
response[5]:NUL
response[6]:LF
response[7]:d
response[8]:i
response[9]:s
response[10]:t
response[11]:r
response[12]:i
response[13]:b
response[14]:u
response[15]:t
response[16]:e

the relevant code on the client side:

HttpClient client = new HttpClient();
url = new URL(sUrl);
String strUrl = url.toExternalForm();
GetMethod method = new GetMethod(strUrl);
DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
retryhandler.setRequestSentRetryEnabled(false);
retryhandler.setRetryCount(1);
method.setMethodRetryHandler(retryhandler);
// Prevent CLOSE_WAITs
method.addRequestHeader( "Connection", "close");
method.setRequestHeader("task", "get_data");
byte[] responseBody;
// Make sure the response body is empty:
try {
  responseBody = method.getResponseBody();
  strDoc = new String(responseBody);
}
catch (Exception exc) {
  ; // just ignore
}
// Execute the method
statusCode = client.executeMethod(method);
// Read the response body
responseBody = method.getResponseBody();
// Release the connection.
method.releaseConnection();
if (statusCode == HttpStatus.SC_OK) {
  File tmpFile = new File(tmpFilename);
  FileOutputStream fos = new FileOutputStream(tmpFile);
  fos.write(responseBody);
}


the relevant code on the server side:

public void handleRequest(HttpServletRequest request, HttpServletResponse
response) throws IOException, ServletException {
  String task = request.getHeader("task");
  if (task != null && task.equalsIgnoreCase("get_data")) {
    getData(request, response);
  }
}

private void getData(HttpServletRequest request, HttpServletResponse
response) {
  ObjectOutputStream oOut = new ObjectOutputStream(response.getOutputStream
());
  if (! configAvailable) {
    response.setContentType("text/plain");
    oOut.writeObject("distribute");
    oOut.flush();
    redistributeRequested = true;
  }
  else {
    response.setContentType("text/xml; charset=ISO-8859-1");
    oOut.writeObject(data);
    oOut.flush();
  }
  oOut.close();
}

Any ideas where the garbage in responsebody comes from ?

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