在使用httpClient.executeMethod(postMethod)的时候,发现一直存在乱码问题,”book is good“被转成”book+is+good“ 返回。查看源码后,发现post这一端底层还会encode,于是就要有一个decode配对才行。
实际上post的底层encode的是是“ ISO8859-1”的方式,之后中文,俄语之类的语种就会被转义成乱码(“Машинка стрижет” 被转义成 %3F%3F%3F%3F%3F%3F%3F+%3F%3F%3F%3F%3F%3F%3F)。
PostMethod postMethod = new PostMethod(ParseRequest.UrlPage(httpUrl));
/* 一定要指定utf-8版本 不然post 默认用iso编码直接会把俄语编码 搞乱 */
postMethod.getParams().setContentCharset("UTF-8");
留下证据:
protected RequestEntity generateRequestEntity() { if (!this.params.isEmpty()) { // Use a ByteArrayRequestEntity instead of a StringRequestEntity. // This is to avoid potential encoding issues. Form url encoded strings // are ASCII by definition but the content type may not be. Treating the content // as bytes allows us to keep the current charset without worrying about how // this charset will effect the encoding of the form url encoded string. String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet()); ByteArrayRequestEntity entity = new ByteArrayRequestEntity( EncodingUtil.getAsciiBytes(content), FORM_URL_ENCODED_CONTENT_TYPE ); return entity; } else { return super.generateRequestEntity(); } }