【问题标题】:URL encode in Erlang and CyrillicErlang 和 Cyrillic 中的 URL 编码
【发布时间】:2012-04-26 14:37:30
【问题描述】:

我在 erlang 中使用标准 httpc 客户端,用于向某些服务发送/接收请求。有时我的 URL 中有一个西里尔字母路径。如何对 Cyrillic URL 进行 URL 编码?

谢谢。

【问题讨论】:

    标签: encoding erlang urlencode


    【解决方案1】:
    ~/erl$erl
    Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
    
    Eshell V5.8.5  (abort with ^G)
    1> Encoded = edoc_lib:escape_uri("абвгдеё").
    "%c0%b0%c0%b1%c0%b2%c0%b3%c0%b4%c0%b5%c1%91"
    2> http_uri:decode(Encoded).
    [192,176,192,177,192,178,192,179,192,180,192,181,193,145]
    

    您可以使用 list_to_binary 作为二进制文件。

    【讨论】:

      【解决方案2】:

      我在使用 edoc lib 中的 escape_uri 时遇到了一些问题。

      所以我最终写了一个适用于 utf-8 的:

      https://stackoverflow.com/a/12648499/113112

      https://gist.github.com/3796470

      例如

      Eshell V5.9.1  (abort with ^G)
      
      1> c(encode_uri_rfc3986).
      {ok,encode_uri_rfc3986}
      
      2> encode_uri_rfc3986:encode("абвгдеё").
      "%d0%b0%d0%b1%d0%b2%d0%b3%d0%b4%d0%b5%d1%91"
      
      3> edoc_lib:escape_uri("абвгдеё").
      "%c0%b0%c0%b1%c0%b2%c0%b3%c0%b4%c0%b5%c1%91" # output wrong: À°À±À²À³À´ÀµÁ‘
      

      【讨论】:

        猜你喜欢
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-12
        • 2013-04-21
        • 2010-09-11
        • 1970-01-01
        相关资源
        最近更新 更多