• 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();
            }
        }
  • 相关阅读:
    什么是字典序算法?
    安装使用zookeeper
    用最快速度将0*10范围内的数进行排序
    自定义schema 流程
    dubbo原理
    jvm 线上命令
    如何实现抢红包算法?
    GC Root 对象有哪些
    Jquery动态绑定事件处理函数 bind / on / delegate
    找出数组中的最小值(es5/es6)
  • 原文地址:https://www.cnblogs.com/huhuuu/p/4762483.html
Copyright © 2020-2023  润新知