1

如果 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>
注册名称:[]
异常错误:
函数 iolist_to_binary/1 中的错误参数
称为 iolist_to_binary(["GET"," ",
[47,109,101,116,104,111,100,47,97,117,
100,105,111115,115,1115,1115,115,115,115,1115,1151015,1115,1115,1151015,1151015,11510515,115.1115
_
_
_
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\nconnection: keep-alive\r\n",
"\r\n",[]])

4

1 回答 1

3

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

Erlang HTTP(S) 模块不包含那个有用的例程可能看起来很奇怪,但可能与模块排列有关(我不是 Erlang 大师)

于 2012-11-27T17:31:27.713 回答