Erlang使用Unicode来解决httpc:request

时间:2012-11-27 17:12:30

标签: unicode erlang inets

如果URL是Unicode字符串,如何使用httpc:request?

get_songs(Findstring) ->
application:start(inets),
application:start(crypto),
application:start(public_key),
application:start(ssl),       
Uf = normalize(Findstring),
Ufs = unicode:characters_to_list(Uf),
Token= "e...2",
Req1="https://api.vk.com/method/audio.search?q="++Ufs++"&access_token="++Token,
case httpc:request(get, {Req1,[]}, [], []) of
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}} ->
    Result = io_lib:format("~ts",[Body]),
    Result;
Resp ->
    Result = io_lib:format("response: ~p ~n", [Resp]),
    Result   
end.

如果我将Findstring发送为Latin1,则代码正常工作。但如果Findstring是Unicode格式,我会收到错误。

  

破碎机:
      初始调用:httpc_handler:init / 1
      pid:< 0.172.0>
      registered_name:[]
      异常错误:坏参数
        在函数iolist_to_binary / 1中            称为iolist_to_binary([“GET”,“”,
                                       [47,109,101,116,104,111,100,47,97,117,
                                        100,105,111,46,115,101,97,114,99,104,63,
                                        1083,1086,1083,38,97,99,99,101,115,115,
                                        95,116,111,107,101,110,61,101,50,102,50,
                                        98,55,53,100,101,50,101,101,52,55,51,48,
                                        101,50,101,101,52,55,51,48,97,51,101,50,
                                        100,102,102,55,54,57,101,101,50,101,102,
                                        101,50,101,49,48,53,48,101,98,50,53,97,
                                        101,50,49,51,50,98,98,55,98,100,56,57,
                                        101,99,55,56,53,101,99,50],
                                       “”,“HTTP / 1.1”,“\ r \ n”,
                                       “te:\ r \ nhost:api.vk.com \ r \ nnconnection:keep-alive \ r \ n”,

                                       为 “\ r \ n” 个,[]])

1 个答案:

答案 0 :(得分:3)

您必须urlencode查询字符串参数,,,

Erlang HTTP(S)模块不包含有用的例程似乎很奇怪,但可能与模块安排有关(我不是Erlang大师)