You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by yl d <ch...@yahoo.com> on 2004/03/14 14:46:06 UTC

i was troubled when using chinese at request body

hi,
i am using apache http client lib to get rusult from
my jsp webpage.
the jsp page get a request like:text=....,and return a
result and show it on web.
when i enter a request like
:http://localhost/a.jsp?text=...(here is some chinese
words) at IE6.0,at the jsp file,i get the request
string ,and i use 
this.userRequest = new
String(this.userRequest.getBytes("8859_1"), "gb2312")
to get the chinese string.(gb2312 is a kind of chinese
encoding)
i succeed.

but when i can use apache http client:
String s="...";(here is some chinese words)
HttpClient httpClient = new HttpClient();
GetMethod get = new GetMethod("http://url/?text="+s);
int responseCode = httpClient.executeMethod(get);
String responseBody = get.getResponseBodyAsString();
System.out.print( responseBody);

i look in the jsp file,it can't print the right
chinese words.(though i have used  new
String(this.userRequest.getBytes("8859_1"), "gb2312")
in the jsp file.) 

i don't know what is the reason and how to do.
i look up in the mail list archive,and i can't find
the answer(someone seems meet the same problem,he use
big5).
can anyone help me?thanks...
sorry for my poor english. 



__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com

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


Re: i was troubled when using chinese at request body

Posted by Oleg Kalnichevski <ol...@apache.org>.
Hi there

Firstly, one part of the problem is that you do not URL encode your
query parameters

This is not sufficient

GetMethod get = new GetMethod("http://url/?text="+s);

Please do the following instead:

GetMethod get = new GetMethod("http://url/");
NameValuePair[] params = new NameValuePair[] {
	new NameValuePair("text", s)
};
get.setQueryString(EncodingUtil.formUrlEncode(params, "gb2312"));

Secondly, my guess it that the web server does not include the charset
in the content-type header, which causes getResponseCharSet method to
return the default charset (iso-8859-1) instead of expected gb2312

See the following document for more details:
<http://jakarta.apache.org/commons/httpclient/charencodings.html>

To verify this assumption you may activate and examine the wire log:
<http://jakarta.apache.org/commons/httpclient/logging.html>

To mend the problem you can simply retrieve the raw response body as
array of bytes and perform the character translation manually with the
desired charset

String responseBody = null;
byte[] rawdata = get.getResponseBody();
if (rawdata != null) {
  responseBody = HttpConstants.getContentString(rawdata, "gb2312");
}

Hope this helps

Oleg



On Sun, 2004-03-14 at 14:46, yl d wrote:
> hi,
> i am using apache http client lib to get rusult from
> my jsp webpage.
> the jsp page get a request like:text=....,and return a
> result and show it on web.
> when i enter a request like
> :http://localhost/a.jsp?text=...(here is some chinese
> words) at IE6.0,at the jsp file,i get the request
> string ,and i use 
> this.userRequest = new
> String(this.userRequest.getBytes("8859_1"), "gb2312")
> to get the chinese string.(gb2312 is a kind of chinese
> encoding)
> i succeed.
> 
> but when i can use apache http client:
> String s="...";(here is some chinese words)
> HttpClient httpClient = new HttpClient();
> GetMethod get = new GetMethod("http://url/?text="+s);
> int responseCode = httpClient.executeMethod(get);
> String responseBody = get.getResponseBodyAsString();
> System.out.print( responseBody);
> 
> i look in the jsp file,it can't print the right
> chinese words.(though i have used  new
> String(this.userRequest.getBytes("8859_1"), "gb2312")
> in the jsp file.) 
> 
> i don't know what is the reason and how to do.
> i look up in the mail list archive,and i can't find
> the answer(someone seems meet the same problem,he use
> big5).
> can anyone help me?thanks...
> sorry for my poor english. 
> 
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail - More reliable, more storage, less spam
> http://mail.yahoo.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org
> 


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