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();
            }
        }
  • 相关阅读:
    TouTiao开源项目 分析笔记7 加载数据的过程
    字符串到-->list到-->字典的转变
    使用golang插入mysql性能提升经验
    linux存储管理之逻辑卷
    三、软件设计原则
    二、uml图-->主要是类图的讲解
    一、设计模式概述
    函数申明和函数表达式
    GCD
    推荐系统(Recommendation System)
  • 原文地址:https://www.cnblogs.com/huhuuu/p/4762483.html
Copyright © 2011-2022 走看看