Hi Oleg,

> StringEntity strEntity = new StringEntity(str);
> 
This constructor assumes default charset encoding for HTTP content which is 
ISO-8859-1.

> strEntity.setContentType("text/plain; charset=" + ENCODING);
> 
This basically causes the content to be decoded incorrectly as long as ENCODING 
is not ISO-8859-1.


Then, what is my option? Something like:

ByteArrayEntity bae = new ByteArrayEntity(str.getBytes(ENCODING));
objPost.setEntity(bae);


Another question: if the StringEntity constructor assumes ISO encoding for the 
String, what's the utility of 'entity.setContentType'?
I expected to find an empty constructor to do something like:
StringEntity strEntity = new StringEntity();
strEntity.setContentType("text/plain; charset=" + ENCODING);
strEntity.setContent(str);


Thanks,
Joan.



-----Mensaje original-----
De: Oleg Kalnichevski [mailto:[email protected]] 
Enviado el: jueves, 12 de enero de 2012 16:52
Para: HttpClient User Discussion
Asunto: Re: Encoding issue

On Thu, 2012-01-12 at 14:15 +0100, Joan Balaguero wrote:
> 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);
> 

This constructor assumes default charset encoding for HTTP content which
is ISO-8859-1.

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

This basically causes the content to be decoded incorrectly as long as
ENCODING is not ISO-8859-1.

Oleg

> 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.
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


-----
No se encontraron virus en este mensaje.
Comprobado por AVG - www.avg.com
Versión: 2012.0.1901 / Base de datos de virus: 2109/4737 - Fecha de 
publicación: 01/11/12


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to