我使用erlang并使用与我的电脑相关的调制解调器
我想从调制解调器发送消息到我的电话号码
我开发了这个功能,它完美地运作
send(To) ->
Message =http_urii:encode("Vous n'avez pas d'écheance à payer."),
Url = io_lib:format("http://192.168.1.5:9998/sendsms?tel=~s&text=~s", [To, Message]),
http:request(lists:flatten(Url), sms).
我的问题与diacritic的符号有关,例如é,à
因为在我的手机中我收到短信但是spécial字符没有以正确的格式显示é成为另一个字符,à成为另一个字符
我希望在手机中收到以下消息: Vous n'avez pas d'écheanceàpayer。
我修改了发送的源代码
send(To) ->
Message =http_urii:encode("Vous n'avez pas d'%E9cheance %EA payer."),
Url = io_lib:format("http://192.168.1.5:9998/sendsms?tel=~s&text=~s", [To, Message]),
http:request(lists:flatten(Url), sms).
但在我的手机中,我收到此消息 Vous n'avez pas d'%E9cheance%EA付款人。没有特殊字符编码
http_urii.erl 的源代码是:
-module(http_urii).
-export([parse/1, encode/1, decode/1]).
%%%=========================================================================
%%% API
%%%=========================================================================
parse(AbsURI) ->
case parse_scheme(AbsURI) of
{error, Reason} ->
{error, Reason};
{Scheme, Rest} ->
case (catch parse_uri_rest(Scheme, Rest)) of
{UserInfo, Host, Port, Path, Query} ->
{Scheme, UserInfo, Host, Port, Path, Query};
_ ->
{error, {malformed_url, AbsURI}}
end
end.
encode(URI) ->
Reserved = sets:from_list([$;, $:, $@, $&, $=, $+, $,, $/, $?,
$#, $[, $], $<, $>, $\", ${, $}, $|,
$\\, $', $^, $%, $ ]),
lists:append(lists:map(fun(Char) ->
uri_encode(Char, Reserved)
end, URI)).
decode([$%,Hex1,Hex2|Rest]) ->
[hex2dec(Hex1)*16+hex2dec(Hex2)|decode(Rest)];
decode([First|Rest]) ->
[First|decode(Rest)];
decode([]) ->
[].
%%%========================================================================
%%% Internal functions
%%%========================================================================
parse_scheme(AbsURI) ->
case split_uri(AbsURI, ":", {error, no_scheme}, 1, 1) of
{error, no_scheme} ->
{error, no_scheme};
{StrScheme, Rest} ->
case list_to_atom(http_util:to_lower(StrScheme)) of
Scheme when Scheme == http; Scheme == https ->
{Scheme, Rest};
Scheme ->
{error, {not_supported_scheme, Scheme}}
end
end.
parse_uri_rest(Scheme, "//" ++ URIPart) ->
{Authority, PathQuery} =
case split_uri(URIPart, "/", URIPart, 1, 0) of
Split = {_, _} ->
Split;
URIPart ->
case split_uri(URIPart, "\\?", URIPart, 1, 0) of
Split = {_, _} ->
Split;
URIPart ->
{URIPart,""}
end
end,
{UserInfo, HostPort} = split_uri(Authority, "@", {"", Authority}, 1, 1),
{Host, Port} = parse_host_port(Scheme, HostPort),
{Path, Query} = parse_path_query(PathQuery),
{UserInfo, Host, Port, Path, Query}.
parse_path_query(PathQuery) ->
{Path, Query} = split_uri(PathQuery, "\\?", {PathQuery, ""}, 1, 0),
{path(Path), Query}.
parse_host_port(Scheme,"[" ++ HostPort) -> %ipv6
DefaultPort = default_port(Scheme),
{Host, ColonPort} = split_uri(HostPort, "\\]", {HostPort, ""}, 1, 1),
{_, Port} = split_uri(ColonPort, ":", {"", DefaultPort}, 0, 1),
{Host, int_port(Port)};
parse_host_port(Scheme, HostPort) ->
DefaultPort = default_port(Scheme),
{Host, Port} = split_uri(HostPort, ":", {HostPort, DefaultPort}, 1, 1),
{Host, int_port(Port)}.
split_uri(UriPart, SplitChar, NoMatchResult, SkipLeft, SkipRight) ->
case inets_regexp:first_match(UriPart, SplitChar) of
{match, Match, _} ->
{string:substr(UriPart, 1, Match - SkipLeft),
string:substr(UriPart, Match + SkipRight, length(UriPart))};
nomatch ->
NoMatchResult
end.
default_port(http) ->
80;
default_port(https) ->
443.
int_port(Port) when is_integer(Port) ->
Port;
int_port(Port) when is_list(Port) ->
list_to_integer(Port).
path("") ->
"/";
path(Path) ->
Path.
uri_encode(Char, Reserved) ->
case sets:is_element(Char, Reserved) of
true ->
[ $% | http_util:integer_to_hexlist(Char)];
false ->
[Char]
end.
hex2dec(X) when (X>=$0) andalso (X=<$9) -> X-$0;
hex2dec(X) when (X>=$A) andalso (X=<$F) -> X-$A+10;
hex2dec(X) when (X>=$a) andalso (X=<$f) -> X-$a+10.
现在我通过标准http_uri
更改http_urii但是当我执行我的项目erlang
时我有这个错误: Erlang R13B03(erts-5.7.4)[来源] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.4 (abort with ^G)
1> exception: [{http_uri,encode,
["Vous n'avez pas d'écheance à payer."]},
{sms_alert,send,1},
{webrpc_api,build_response,2},
{webrpc_api,format_response,2},
{webrpc_api,process_command,2},
{httpd_response,traverse_modules,2},
{httpd_response,generate_and_send_response,1},
{httpd_request_handler,handle_response,1}]
我知道我使用的是erlang版本13,但我在我的项目的src中创建了模块http_uri.erl,所以通常它应该工作
答案 0 :(得分:0)
如果您查看http_urii:encode/1
返回的内容,您会发现它不是您明确使用的内容。在我的机器上,我得到:
4> http_urii:encode("Vous n'avez, pas d'énheance à payer.").
"Vous%20n%27avez%2C%20pas%20d%27énheance%20à%20payer."
标准的Erlang版本中有一个模块http_uri
,在这种情况下返回与http_urii
相同的内容。