【问题标题】:Change charset of a response body from UTF-8 to CP1251将响应正文的字符集从 UTF-8 更改为 CP1251
【发布时间】:2014-12-29 08:19:37
【问题描述】:

我评估以下代码

(org.httpkit.client/get "http://localhost:81"
                    #(clojure.pprint/pprint (.getBytes (:body %))))

打印出来

[-17, -65, -67, -17, -65, -67]

如果 index.html 在 CP1251 中,并且

[-48, -80, -48, -79, -48, -78]

如果相同的文档是 UTF-8。

index.html 的俄文内容是

абв

http-kit 将响应正文作为 UTF-8 编码的字符串对象返回,但它不考虑 HTML 文档的实际字符集。这会导致身体中的垃圾像

"<html>�����</html>"

如何让 org.httpkit.client/get 看文件的字符集?

【问题讨论】:

    标签: clojure http-kit


    【解决方案1】:

    您可以通过使用带有特定选项的 org.httpkit.client.request 来获取正文的原始字节。

    如果文档采用 CP1251 编码,则以下代码将打印正确的正文内容。

    (org.httpkit.client/request {:url "http://localhost:81" :as :byte-array} 
                                #(println (String. (:body %) "cp1251")))
    

    【讨论】:

      猜你喜欢
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2019-01-24
      • 2010-11-12
      • 2012-08-26
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多