hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <oleg.kalnichev...@bearingpoint.com>
Subject Re: UTF-8 Encoding Enquiry
Date Wed, 13 Oct 2004 11:45:26 GMT

Ramiel,
I think you came pretty close. Two things require minor corrections

(1) Content-Type

HTTP POST per default uses so called URL encoding when submitting HTML
forms. Setting the content type to 'text/plain' may cause some web
servers to misinterpret the request parameters

Try this instead

postmethod.setRequestHeader(
 "Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

(2) Request parameters

This operation is completely redundant

new String(((String)
paramList.get(paramName)).getBytes("UTF-8"),"UTF-8")

As far as I understand it produces exactly the same Unicode string

// Assign parameters into post method
Iterator it = paramList.keySet().iterator();
while (it.hasNext()) {
 String paramName = (String) it.next();
 postmethod.addParameter(paramName, paramList.get(paramName));
}

HttpClient will do all the charset conversion for you. Just make sure the charser attribute
of the Content-Type is set

For details please refer to the HttpClient encoding guide

http://jakarta.apache.org/commons/httpclient/charencodings.html

Hope this helps

Oleg

On Wed, 2004-10-13 at 13:22, Ramiel Fong wrote:
> Hi~!
>
> Thanks in advance for those who will be looking into my problem.
>
> I am having difficulty in sending chinese characters via postmethod.
>
> // Create an instance of HttpClient
> HttpClient httpclient = new HttpClient();
>
> // Create a method instance
> PostMethod postmethod = new PostMethod(url);
>
> // Assign parameters into post method
> Iterator it = paramList.keySet().iterator();
> while (it.hasNext()) {
>   String paramName = (String) it.next();
>     postmethod.addParameter(paramName,
>       new String(((String) paramList.get(paramName)).getBytes("UTF-8"),"UTF-8"));
> }
>
> With the above codes I succeeded in sending out the postmethod with the list of parameters.
 But all chinese characters become "?" upon received by back-end.
>
> Therefor I add this:
>
> // Set request header - character set
> postmethod.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
>
> With the above lines added, the back-end server could receive the postmethod request
but can only find an empty parameter list.
>
> The back-end server is indeed a servlet resided on websphere 5 server.  The codes to
get the one of the parameter is:
>
> request.setCharacterEncoding("UTF-8");
> String inXMLMessage = (String) request.getParameter("xmlmessage");
>
> Since the parameter list become empty the above method failed.  Therefore I switched
to another method - to get the entire request body with input stream and added the following
codes:
>
> if (inXMLMessage == null)
> inXMLMessage = getXMLMessageFromInputStream (request.getInputStream());
>
> ......
>
> public String getXMLMessageFromInputStream (InputStream requestIS) {
>   String XMLMessage = "";
>     try {
>       InputStreamReader isr = new InputStreamReader(requestIS, "UTF-8");
>       BufferedReader r = new BufferedReader(isr);
>       XMLMessage = r.readLine();
>       XMLMessage = URLDecoder.decode(XMLMessage);
>       XMLMessage = XMLMessage.substring(11);
>   } catch (Exception ex) {...}
>   return XMLMessage;
> }
>
> With the above I succeeded to get the parameter I want, i.e. an xml message, from the
input stream, but the entire message are "escaped", that is space and tab become "+", "<"
become "%3C", etc.  Thaz why I need to use URLDecoder .  But the worst part is that all chinese
charaters become weird codes like &aelig;&cedil;&not;&egrave;&copy;&brvbar;,
which should be "測試" in traditional chinese.
>
> I am really at my wits end as to what is happening here.  Would somebody throw a light
upon my proble?  A thousand thanks in advance!
>
> Regards
> Ramiel Fong
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org

***************************************************************************************************
The information in this email is confidential and may be legally privileged.  Access to this
email by anyone other than the intended addressee is unauthorized.  If you are not the intended
recipient of this message, any review, disclosure, copying, distribution, retention, or any
action taken or omitted to be taken in reliance on it is prohibited and may be unlawful. 
If you are not the intended recipient, please reply to or forward a copy of this message to
the sender and delete the message, any attachments, and any copies thereof from your system.
***************************************************************************************************

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


Mime
View raw message