zoukankan      html  css  js  c++  java
  • httpClenit的post出现乱码问题

      在使用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();
            }
        }
  • 相关阅读:
    P2167 [SDOI2009]Bill的挑战
    P2153 [SDOI2009]晨跑
    洛咕 P2155 [SDOI2008]沙拉公主的困惑
    洛咕 P2465 [SDOI2008]山贼集团
    洛咕 P2463 [SDOI2008]Sandy的卡片
    洛咕 P4556 [Vani有约会]雨天的尾巴
    CF418D Big Problems for Organizers
    CF28D Don't fear, DravDe is kind
    CF97C Winning Strategy
    P4427 [BJOI2018]求和
  • 原文地址:https://www.cnblogs.com/huhuuu/p/4762483.html
Copyright © 2011-2022 走看看