hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joan Balaguero" <joan.balagu...@grupoventus.com>
Subject Encoding issue
Date Thu, 12 Jan 2012 13:15:00 GMT
Hello Oleg,

 

I’m having an strange issue with encoding using HttpClient 4.0.1.

 

I’m sending a string to a servlet using HttpClient 4.0.1. The servlet just
sends back to the client exactly the same received string:

 

This is the piece of code that sends the string to the servlet and receives
the response:

 

( . . . )

 

String ENCODING = “ISO-8859-1”;

String str   = "ÑÑÑ_ÁÁÁ";

 

objPost = new HttpPost(url);

 

StringEntity strEntity = new StringEntity(str);

strEntity.setContentType("text/plain; charset=" + ENCODING);

objPost.setEntity(strEntity);

                    

HttpEntity entity = null;

 

try 

{ 

 entity = objHttp.execute(objPost).getEntity(); 

 

// Read the response.

BufferedInputStream bis   = new BufferedInputStream(entity.getContent());

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] buffer = new byte[4098];

int numBytes;

 

while ((numBytes = bis.read(buffer)) >= 0) bos.write(buffer, 0, numBytes);

                     

 System.out.println("RESPONSE = " + new String(bos.toString(ENCODING));

 

( . . . )

 

 

And this is the piece of code of the servlet that prints the request
inputstream directly to the response outputstream:

 

( . . . )

 

   BufferedOutputStream bos = null;

   

   try

   {

    response.setContentType(request.getContentType());

 

    bos                 = new
BufferedOutputStream(response.getOutputStream());

    InputStream in      = request.getInputStream();

    byte[] tmp   = new byte[4098];

    int numBytesRead = 0;

    while ((numBytesRead = in.read(tmp, 0, 4098)) >= 0) bos.write(tmp, 0,
numBytesRead);

 

( . . . )

 

 

If the ENCODING variable is “ISO-8859-1”, the response is received OK:

RESPONSE = ÑÑÑ_ÁÁÁ

 

But if the ENCODING variable is “UTF-8”, the response is received KO:

RESPONSE = "???_???"

In this case, if I force an “iso” decoding in the line:

System.out.println("RESPONSE = " + new String(bos.toString(“ISO-8859-1”));

Then it works: RESPONSE = ÑÑÑ_ÁÁÁ

 

 

Could be an issue in StringEntity? Or maybe I’m missing anything?

 

Thanks in advance,

 

Joan.


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