使用erlang中的特殊字符发送消息
我和erlang一起工作,我使用与我的电脑相关的调制解调器 我想把信息从调制解调器发送到我的电话号码 我开发了这个函数,它工作得很好使用erlang中的特殊字符发送消息,erlang,Erlang,我和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
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).
我的问题与符号有关,例如,é,a
因为在我的手机中,我收到了短信,但是特殊字符没有以正确的格式显示,变成了另一个字符,变成了另一个字符
我想在我的手机里收到这样一条信息:你是付款人。
我修改了send的源代码
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).
但是在我的手机中,我收到了这条信息,没有特殊字符编码
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.
我知道我使用的是erlang版本13,但是我在我的项目的src中创建了模块http_uri.erl,所以通常应该使用它,如果您查看http_uri: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\u uri
,在本例中返回与您的http\u urii
相同的内容。您可以包含sendsms
程序中的相关代码吗?您在系统的每个部分使用什么字符编码?我同意sendsms似乎是问题的一个可能来源。我建议将此请求指向web服务器,您可以在该服务器上看到请求日志,以确保正确表示变音符号。不熟悉http_urii库,或者这是打字错误?
4> http_urii:encode("Vous n'avez, pas d'énheance à payer.").
"Vous%20n%27avez%2C%20pas%20d%27énheance%20à%20payer."